0% found this document useful (0 votes)
10 views19 pages

Taha Muzaffar OOP Assignment#2

Uploaded by

arslanzero234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views19 pages

Taha Muzaffar OOP Assignment#2

Uploaded by

arslanzero234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

OBJECT ORIENDTED PROGRAMMING TAHA MUZAFFAR

Assignment# 02 02-132222-036

BAHRIA UNIVERSITY, (Karachi


Campus)
Department of Computer Engineering
Assignment # 2 – Fall 2023

COURSE TITLE: Object-Oriented Programming


COURSE CODE: CSC-210
CLO#: 1 [C2: Comprehension] PLO#:2[Engineering Problem Analysis]

Class BCE-3 Shift: Morning


Course Instructor: Engr. Usra Sami Time Allowed: 1 Week
Max. Marks: 5 Marks

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.

Name: TAHA MUZAFFAR Enrollment No: 02-132222-036

Task 1:

A vehicle company manufactures cars. In recent years company produced a car


called ‘L-Sedan’ which has blue color, it’s engine type is gas, seating capacity is 5.
Identify the object, form the class and make a test application.

Answer:

SOURCE CODE:
class Car {
private String model;
private String color;
private String engineType;
private int seatingCapacity;

Car(String model, String color, String engineType, int seatingCapacity) {


this.model = model;
this.color = color;
this.engineType = engineType;

1
OBJECT ORIENDTED PROGRAMMING TAHA MUZAFFAR
Assignment# 02 02-132222-036

this.seatingCapacity = seatingCapacity;
}

public String getModel() {


return model;
}

public String getColor() {


return color;
}

public String getEngineType() {


return engineType;
}

public int getSeatingCapacity() {


return seatingCapacity;
}
}

public class CarTest {


public static void main(String[] args) {

Car lSedan = new Car("L-Sedan", "Blue", "Gas", 5);

System.out.println("Car Model: " + lSedan.getModel());


System.out.println("Car Color: " + lSedan.getColor());
System.out.println("Engine Type: " + lSedan.getEngineType());
System.out.println("Seating Capacity: " + lSedan.getSeatingCapacity());
}
}

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;

public OOPCourse(String name, int id) {


studentName = name;
rollNumber = id;
}

3
OBJECT ORIENDTED PROGRAMMING TAHA MUZAFFAR
Assignment# 02 02-132222-036

public void displayStudentInfo() {


System.out.println("Student Name: " + studentName);
System.out.println("Roll Number: " + rollNumber);
}
}

public class OOPCourseDemo {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter your name: ");


String name = scanner.nextLine();

System.out.print("Enter your roll number: ");


int id = scanner.nextInt();

OOPCourse student = new OOPCourse(name, id);

System.out.println("Entered Student Information:");


student.displayStudentInfo(); } }

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

public static void main(String[] args) {


Student student = new Student(); student.InputInfo(); student.Display();
double percentage = student.CalculatePercentage();
System.out.println("Percentage: " + percentage + "%");
}
}

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;

public GradeBook(String courseName, String courseInstructor) {


this.courseName = courseName;
this.courseInstructor = courseInstructor;
}

public void displayMessage() {


System.out.println("Welcome to the course: " + courseName);
System.out.println("This course is presented by: " + courseInstructor);
}

public static void main(String[] args) {

GradeBook myGradeBook = new GradeBook("Introduction to Java


Programming", "John Smith");

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;

// Default constructor with hardcoded radius


Circle() {
radius = 5.0; // Default radius value
}

// Constructor with user-defined radius


Circle(double userRadius) {
radius = userRadius;
}

// Method to calculate and return the area of the circle


public double calculateArea() {
return Math.PI * radius * radius;
}
}

public class CircleDemo {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Choose a constructor for the Circle class:");


System.out.println("1. Default constructor (hardcoded radius)");

8
OBJECT ORIENDTED PROGRAMMING TAHA MUZAFFAR
Assignment# 02 02-132222-036

System.out.println("2. User-defined constructor (provide radius)");

int choice = scanner.nextInt();

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();
}

System.out.println("Area of the circle: " + circle.calculateArea());


}
}

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;

public class Student{


String id;
String rollNumber; 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();
}
public void Display() { System.out.println("\nStudent Information:");
System.out.println("ID: " + id); System.out.println("Roll Number: " +
rollNumber);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Student student = new Student(); student.InputInfo(); student.Display();
}
}

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)");

System.out.println("2. Parameterized Constructor (User Input


for Radius)"); choice = input.nextInt();

12
OBJECT ORIENDTED PROGRAMMING TAHA MUZAFFAR
Assignment# 02 02-132222-036

// Use the default constructor with a


hardcoded radius circle = new Circle();
System.out.print("Enter the radius
of the circle: "); int userRadius =
input.nextInt();
circle = new Circle(userRadius); // Use the
parameterized constructor break;
default:
System.out.println("Invalid choice. Using the default constructor.");
circle = new Circle(); // Using the default constructor in case of an
invalid choice break;
}

// 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

this.rollNo = rollNo; this.percentage = percentage;


}
// Method to show the information of a student public void displayStudentInfo()
{ System.out.println("Student Name: " + name);
System.out.println("Roll Number: " + rollNo);
System.out.println("Percentage: " + percentage + "%");
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Student student1 = new Student();
student1.name = "John Doe"; student1.rollNo = 12345;
student1.displayStudentInfo(); System.out.println();
// Create a student with name, roll no, and percentage using the parameterized
constructor Student student2 = new Student("Jane Smith", 67890, 85.5);
student2.displayStudentInfo();
}
}

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;

public Author(String n, char g) {


this.name = n;
this.gen = g;
}

public String getName() {


return name;
}

public char getGen() {

17
OBJECT ORIENDTED PROGRAMMING TAHA MUZAFFAR
Assignment# 02 02-132222-036

return gen;
}

public void setEmail(String e) {


this.email = e;
}

public String getEmail() {


return email;
}

public void print() {


System.out.println("name: " + name);
System.out.println("gen: " + gen);
}

public static void main(String[] args) {


Author d1 = new Author("Taha", 'm');

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

You might also like