Java OOPs Practice Programs for Beginners

In this post, I will be sharing Java OOPs practice programs i.e. coding questions. I have already shared the OOPs concepts in detail here. Before diving deep into the topic, please bookmark this page as I will keep adding more questions to it:

Read Also: OOPs Interview Questions and Answers

Q1 Create a 'Student' class with variables 'firstName', 'rollNumber', and 'lastName'. Create an object of Student class with firstName as 'ABC', rollNumber as '123456', and 'lastName' as 'XYZ'?

public class Student {
    public String firstName;
    public int rollNumber;
    public String lastName;
    
    public Student(String firstName, int rollNumber, String lastName)
    {
        this.firstName = firstName;
        this.rollNumber = rollNumber;
        this.lastName = lastName;
    }
    
    public static void main(String args[])
    {
        Student stu = new Student("ABC", 123456, "XYZ");
    }
}


Q2 Create three objects of Student class with variables 'address', 'rollNumber', and 'phoneNumber'. Assign and print the rollNumber, address, and phoneNumber of three Student objects with arbitrary values.

public class Student {
    public String address;
    public int rollNumber;
    public String phoneNumber;
    
    public Student(String address, int rollNumber, String phoneNumber)
    {
        this.address = address;
        this.rollNumber = rollNumber;
        this.phoneNumber = phoneNumber;
    }
    
    public static void main(String args[])
    {
        Student stu1 = new Student("123 StreetName1 CityName1, USA", 123456, "123-456-789");
        Student stu2 = new Student("456 StreetName2 CityName2, USA", 234567, "456-789-123");
        Student stu3 = new Student("789 StreetName3 CityName3, USA", 345678, "789-123-456");
        System.out.println("Student1: "+ stu1.address+" "+stu1.rollNumber+" "+stu1.phoneNumber);
        System.out.println("Student2: "+ stu2.address+" "+stu2.rollNumber+" "+stu2.phoneNumber);
        System.out.println("Student3: "+ stu3.address+" "+stu3.rollNumber+" "+stu3.phoneNumber);
    }
}


Output:
Student1: 123 StreetName1 CityName1, USA 123456 123-456-789
Student2: 456 StreetName2 CityName2, USA 234567 456-789-123
Student3: 789 StreetName3 CityName3, USA 345678 789-123-456


Q3 Write a simple Java program to find the area of rectangle by creating a class named 'RectangleArea' having two methods. First method is named 'setDimension' that sets length and breadth of rectangle and the second method named as 'calculateArea' that returns the desired output i.e. area of the rectangle. Length and breadth must be entered through the keyboard.

import java.util.Scanner;
public class RectangleArea {
    private static double length;
    private static double breadth;
    public static void main(String args[]) {
        setDimension();
        double recArea = calculateArea(length, breadth);
        System.out.println(recArea);
    }
    
    public static double calculateArea(double length, double breadth)
    {
        return length * breadth;
    }
    
    public static void setDimension()
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the length of the rectangle: ");
        length = scan.nextDouble();
        System.out.println("Enter the breadth of the rectangle: ");
        breadth = scan.nextDouble();
    }
}


Output:
Enter the length of the rectangle: 12.0
Enter the breadth of the rectangle: 13.0

156.0

Q4 Create a class to calculate and print the area of a triangle with base 4 and height 3 units. Do not pass any parameters in the constructor.

public class Triangle {
    
    public static double base = 4;
    public static double height = 3;
    
    public static void main(String args[])
    {
        System.out.println("Area of Triangle: "+ calculateTriangleArea());
    }
    
    public static double calculateTriangleArea()
    {
        return ((0.5d) * Triangle.base * Triangle.height);
        
    }
}


Output:
Area of Triangle: 6.0


Q5 Create a class named 'Average' such that it will calculate and print the average of three numbers entered by the user.

import java.util.Scanner;
public class Average {
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the first number: ");
        int num1 = scan.nextInt();
        System.out.println("Enter the second number: ");
        int num2 = scan.nextInt();
        System.out.println("Enter the third number: ");
        int num3 = scan.nextInt();
        int average = calculateAverage(num1, num2, num3); 
        System.out.println("Average of three input numbers is: " + average);
    }
    
    public static int calculateAverage(int num1, int num2, int num3)
    {
        return (num1 + num2 + num3)/3;
    }
}


Output:
Enter the first number: 12
Enter the second number: 13
Enter the third number: 16
Average of three input numbers is: 13


Q6 Create a class 'Employee' with below requirements and print the final salary:
1. getInfo() method that takes the number of hours of work per day and salary of employee as parameter.
2. addSalary() method that will add 10$ to the salary of the employee if it is less than 200$.
3. additionalWork() method that will add 5$ to the salary of the employee if the hours spent on work is more than 6 hours.

Q7 Create a class named 'Matrix' containing constructor that initializes the number of rows and number of columns of a new Matrix object.
The 'Matrix' class has the following information:
1. number of columns of matrix
2. number of rows of matrix
3. elements of matrix in the two-dimensional array
Print the Matrix.

Q8 Continue with Question7, add two matrices. If the matrices can not be added then display message "Matrices can not be added".

Q9 Continue with Question7, multiply two matrices. If the matrices can not be multiplied then display message "Matrices can not be multiplied".

Q10 Create the class 'Account' with data member 'balance'. Create two constructors(two-arguments and no argument) and methods to withdraw and deposit balance.

That's all for today. Please mention in the comments if you have any questions related to the Java OOPs practice programs for beginners.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry