📢 Join My WhatsApp Channel JavaWithSumit

Get Daily ICSE Java Notes, Programs & Exam Tips 🚀

👉 Join Now

Top Java OOP Programs with Output | Class Variable, Encapsulation, Employee Salary, Basic Java Programs

Chapter 2: Programs 



Question 1: WAP to add two integers by using class variable.

Solution: 


class AddNumbers { static int num1 = 10; static int num2 = 20; static int sum; public static void main(String[] args) {
        sum=num1+num2; System.out.println("Sum= "+sum); } }

Output: 30
<>

Question 2: WAP to subtract a float number from an integer number and get the result in integer by using local variable.

Solution: 

class Subtract { public static void main(String[] args) { // Local variables int num1 = 20; float num2 = 5.5f; // Subtraction and type casting to integer int result = (int)(num1 - num2); // Output System.out.println("Result = " + result); } }

Output: 14

Question 3: WAP to print your name by creating an object.

Solution: 

class MyName { // Method to print name void display() { System.out.println("My name is Java Programming"); } public static void main(String[] args) { // Object creation MyName obj = new MyName(); // Method call obj.display(); } }

Output: My name is Java Programming

Question 4: WAP in Java with two private instance variable and create two different functions to get and set the value of those variables. Create the object of that class and show the use of those two functions to get and set the value in main function.

Solution: 

class Student { // Private instance variables private int id; private String name; // Setter methods (set value) public void setData(int i,String name) { id = i; this.name=name; } // Getter methods (get value) public void getData() { System.out.println("Id: "+id); System.out.println("Name: "+name); } public static void main(String[] args) { // Object creation Student s = new Student(); // Setting values using setter s.setData(101,"Computer"); s.getData(); } }

Output: Id: 101
              Name: Computer

Question 5: WAP with an "Employee" class having two variables one for the name of employee (String) and other for base salary(float). Create a function that calculate the total salary by adding 50% HRA to the salary and returns. Create an object of the class in main function and set the name of the employee, call the salary function and print the calculated salary returned by the function with name of the employee.

Solution: 

import java.util.Scanner; class Employee { String name; float baseSalary; // Function to calculate total salary float calculateSalary() { float hra = 0.5f * baseSalary; return baseSalary + hra; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Object creation Employee emp = new Employee(); // Taking input System.out.print("Enter Employee Name: "); emp.name = sc.nextLine(); System.out.print("Enter Base Salary: "); emp.baseSalary = sc.nextFloat(); // Function call float total = emp.calculateSalary(); // Output System.out.println("\nEmployee Name: " + emp.name); System.out.println("Total Salary (with 50% HRA): " + total); sc.close(); } }

Input: Enter Employee Name: Java
           Enter Base Salary: 20000

Output: Employee name: Java
              Total Salary (with 50% HRA): 30000.0

Question 6: Define a class with following members: 
    Data Members: 
        Integer type num1
        Floating point num2
        Double num3
    Functions(just outline):
        getData():        takes no argument and has int return value
        calc():              takes double argument and return a double
        sum():             takes two int arguments and returns an int type 

Solution: 

import class MyClass { // Data Members int num1; float num2; double num3; // Function: getData() int getData() { return 0; } // Function: calc() double calc(double x) { return 0.0; } // Function: sum() int sum(int a, int b) { return 0; } }


Read also:
👉Chapter- 3:User define Methods

Comments

Popular posts from this blog

Introduction to Object Oriented Programming in Java

Important Java Programs for Beginners | Grade, Fibonacci, Armstrong, Pattern & More

Object Modelling – Entities and Their Behaviour in Java