Java Programs with OOP - Questions and
Solutions (Extended)
Program 1: Class with Integer Data Member
Question: Write a program that declares a class with one integer data member and two member functions, in()
to input data and out() to output data in the data member.
Solution:
import java.util.Scanner;
class Test {
private int n;
public void in() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number:");
n = sc.nextInt();
}
public void out() {
System.out.println("The value of n = " + n);
}
}
public class Main {
public static void main(String[] args) {
Test obj = new Test();
obj.in();
obj.out();
}
}
Output:
Enter a number: 10
The value of n = 10
Program 2: Marks Class with Sum and Average
Question: Write a class Marks with three data members to store three marks. Write three member functions:
in() to input marks, sum() to calculate and return the sum, and avg() to calculate and return the average.
Solution:
import java.util.Scanner;
class Marks {
private int mark1, mark2, mark3;
public void inputMarks() {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter three marks:");
mark1 = scanner.nextInt();
mark2 = scanner.nextInt();
mark3 = scanner.nextInt();
}
public int calculateSum() {
return mark1 + mark2 + mark3;
}
public float calculateAverage() {
return (mark1 + mark2 + mark3) / 3.0f;
}
public void displayResult() {
int sum = calculateSum();
float average = calculateAverage();
System.out.println("Sum = " + sum);
System.out.println("Average = " + average);
}
}
public class Main {
public static void main(String[] args) {
Marks marks = new Marks();
marks.inputMarks();
marks.displayResult();
}
}
Output:
Enter three marks: 50 40 50
Sum = 140
Average = 46.666668
Program 3: Circle Class with Area and Circumference
Question: Write a class Circle with one data member radius. Write three member functions: get_radius() to set
the radius value with a parameter, area() to display the area, and circum() to calculate and display the
circumference.
Solution:
import java.util.Scanner;
class Circle {
private float radius;
public void get_radius(float r) {
radius = r;
}
public void area() {
System.out.println("Area of circle: " + 3.14 * radius * radius);
}
public void circum() {
System.out.println("Circumference of circle: " + 2 * 3.14 * radius);
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Circle c = new Circle();
System.out.println("Enter radius:");
float rad = sc.nextFloat();
c.get_radius(rad);
c.area();
c.circum();
}
}
Output:
Enter radius: 5
Area of circle: 78.5
Circumference of circle: 31.4
Program 4: Book Class with Details
Question: Write a class Book with three data members: BookID, Pages, and Price. It should include functions
get() to input values, show() to display details, and set() to set the values using parameters. Also, add a
getPrice() function to return the price. Create two objects of the class and display details of the most expensive
book.
Solution:
import java.util.Scanner;
class Book {
private int BookID, Pages;
private float Price;
public void get() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Book ID:");
BookID = sc.nextInt();
System.out.println("Enter Pages:");
Pages = sc.nextInt();
System.out.println("Enter Price:");
Price = sc.nextFloat();
}
public void show() {
System.out.println("BookID = " + BookID);
System.out.println("Pages = " + Pages);
System.out.println("Price = " + Price);
}
public void set(int id, int pg, float pr) {
BookID = id;
Pages = pg;
Price = pr;
}
public float getPrice() {
return Price;
}
}
public class Main {
public static void main(String[] args) {
Book b1 = new Book();
Book b2 = new Book();
b1.get();
b2.set(2, 320, 150.75f);
System.out.println("Details of the most expensive book:");
if(b1.getPrice() > b2.getPrice()) {
b1.show();
} else {
b2.show();
}
}
}
Output:
Enter Book ID: 1
Enter Pages: 250
Enter Price: 125.20
Details of the most expensive book:
BookID = 2
Pages = 320
Price = 150.75