Taha Muzaffar OOP Assignment#2
Taha Muzaffar OOP Assignment#2
Assignment# 02 02-132222-036
Tasks:
Recognize the topics you have learned in the course so far and solve given (in lecture objects
and classes) questions. Develop and run the programs on NetBeans and submit codes along
with the output screen.
Task 1:
Answer:
SOURCE CODE:
class Car {
private String model;
private String color;
private String engineType;
private int seatingCapacity;
1
OBJECT ORIENDTED PROGRAMMING TAHA MUZAFFAR
Assignment# 02 02-132222-036
this.seatingCapacity = seatingCapacity;
}
2
OBJECT ORIENDTED PROGRAMMING TAHA MUZAFFAR
Assignment# 02 02-132222-036
OUTPUT:
Task 2:
In this OOP class, there are 30 students. And each student has its name and roll
Number .You are required to make a class OOPCourse containing two instance
variable(attributes/data members/member variables ).
Ask the user to provide his name and id .And display the entered information to user.
Answer:
SOURCE CODE:
import java.util.Scanner;
class OOPCourse {
private String studentName;
private int rollNumber;
3
OBJECT ORIENDTED PROGRAMMING TAHA MUZAFFAR
Assignment# 02 02-132222-036
4
OBJECT ORIENDTED PROGRAMMING TAHA MUZAFFAR
Assignment# 02 02-132222-036
OUTPUT:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Task 3:
Declare another method in above example which computes result of a student by
using formula.
(percentage=(obtainMarks/TotalMarks)*100).
Signature of method reflects the example of a method with return value
2) Take example Student and define a method .Ask user to input data in main
Display():which show the user his information.
Answer:
SOURCE CODE:
import java.util.Scanner;
public class Student {
String id;
String rollNumber;
double obtainMarks;
double totalMarks;
public void InputInfo() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your ID: ");
id = scanner.nextLine();
System.out.print("Enter your Roll Number: ");
rollNumber = scanner.nextLine();
System.out.print("Enter obtained marks: ");
obtainMarks = scanner.nextDouble();
System.out.print("Enter total marks: ");
totalMarks = scanner.nextDouble();
}
public void Display() {
System.out.println("\nStudent Information:");
System.out.println("ID: " + id);
System.out.println("Roll Number: " + rollNumber);
}
public double CalculatePercentage() { return (obtainMarks / totalMarks) * 100;
5
OBJECT ORIENDTED PROGRAMMING TAHA MUZAFFAR
Assignment# 02 02-132222-036
OUTPUT:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Task 4:
Create a class GradeBook
a)Include a courseName ,courseInstructor as member variable of type string
b)Provide a constructor to specify two parameters-one for the courseName and one
for the instructor’Name
c)Declare a method displayMessage to output the welcome message and coursename
, followed by “This course is presented by:” and the instructor’s name.
6
OBJECT ORIENDTED PROGRAMMING TAHA MUZAFFAR
Assignment# 02 02-132222-036
Answer:
SOURCE CODE:
public class GradeBook {
private String courseName;
private String courseInstructor;
myGradeBook.displayMessage();
}
}
7
OBJECT ORIENDTED PROGRAMMING TAHA MUZAFFAR
Assignment# 02 02-132222-036
OUTPUT:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Task 5:
Modify the class Circle which has a default constructor in which a hardcode value
sets as a value of a radius. Now apply if and switch control structures for choice to
choose which constructor user wants to use.
Answer:
SOURCE CODE:
import java.util.Scanner;
class Circle {
private double radius;
8
OBJECT ORIENDTED PROGRAMMING TAHA MUZAFFAR
Assignment# 02 02-132222-036
Circle circle;
switch (choice) {
case 1:
// Use the default constructor with the hardcoded radius
circle = new Circle();
break;
case 2:
// Prompt the user to enter a radius for the user-defined constructor
System.out.print("Enter the radius for the circle: ");
double userRadius = scanner.nextDouble();
circle = new Circle(userRadius);
break;
default:
System.out.println("Invalid choice. Using the default constructor.");
circle = new Circle();
}
9
OBJECT ORIENDTED PROGRAMMING TAHA MUZAFFAR
Assignment# 02 02-132222-036
OUTPUT:
10
OBJECT ORIENDTED PROGRAMMING TAHA MUZAFFAR
Assignment# 02 02-132222-036
Task 6:
(Student).
Declare a method as
InputInfo(): to take input data(Id and Roll Number) from user.
Also declare another method
Display():which show the user his information.
Answer:
SOURCE CODE:
import java.util.Scanner;
11
OBJECT ORIENDTED PROGRAMMING TAHA MUZAFFAR
Assignment# 02 02-132222-036
OUTPUT:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Task 7:
Modify the class Circle which has a default constructor in which a hardcode value
sets as a value of a radius. Now apply if and switch control structures for choice to
choose which constructor user wants to use.
Answer:
SOURCE CODE:
import java.util.Scanner;
public class Circle {
int radius;
// Default constructor with a hardcoded radius value Circle() {
radius = 5; // Default radius value
}
// Parameterized constructor to set the radius based on user input Circle(int
userRadius) {
radius = userRadius;
}
// Function to calculate and return the diameter int getDiameter() {
return 2 * radius;
}
double getArea() {
return Math.PI * radius * radius;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Choose a constructor:");
System.out.println("1. Default Constructor (Hardcoded Radius)");
12
OBJECT ORIENDTED PROGRAMMING TAHA MUZAFFAR
Assignment# 02 02-132222-036
// Calculate the
diameter and area int
diameter =
circle.getDiameter();
double area =
circle.getArea();
// Print the results
System.out.println("Diameter of the circle: "
+ diameter); System.out.println("Area of the
circle: " + area);
input.close();
}
}
13
OBJECT ORIENDTED PROGRAMMING TAHA MUZAFFAR
Assignment# 02 02-132222-036
14
OBJECT ORIENDTED PROGRAMMING TAHA MUZAFFAR
Assignment# 02 02-132222-036
OUTPUT:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Task 8:
Define a class Student that has attributes Name, Roll no, (integer),
percentage(double) . Include following constructors:
Create a student with Name and Roll no. Hint: A default constructor that takes no
arguments
Create a student with Name, Roll no,percentage.
Hint: A constructor that takes values of all attributes. Provide a method that shows
the information of a student.
Answer:
SOURCE CODE:
public class Student { String name;
int rollNo;
double percentage;
// Default constructor with no arguments public Student() {
// Initialize with default values or leave them empty name = "";
rollNo = 0;
percentage = 0.0;
}
// Constructor that takes values for all attributes
public Student(String name, int rollNo, double percentage) { this.name = name;
15
OBJECT ORIENDTED PROGRAMMING TAHA MUZAFFAR
Assignment# 02 02-132222-036
OUTPUT:
16
OBJECT ORIENDTED PROGRAMMING TAHA MUZAFFAR
Assignment# 02 02-132222-036
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Task 9:
Example of Access Modifiers
Answer:
SOURCE CODE:
public class Author {
String name;
String email;
char gen;
17
OBJECT ORIENDTED PROGRAMMING TAHA MUZAFFAR
Assignment# 02 02-132222-036
return gen;
}
d1.getName();
d1.getGen();
d1.setEmail("[email protected]");
d1.getEmail();
d1.print();
}
}
18
OBJECT ORIENDTED PROGRAMMING TAHA MUZAFFAR
Assignment# 02 02-132222-036
OUTPUT:
19