Programs on Class as the Basis of all Computation
- Categories Uncategorized
Here we are going to cover some programs which require you to design a class and implement methods as per the questions given.
1 . Make a class circle to find the area of a circle using formula: area = π x radius2
The instance variables of class are radius and PI. Its member functions are as following:
- circle( float R): to store value of ‘R’ to variable radius and assign 3.14 to PI.
- float FindArea( ): to calculate the area of circle and return.
Write a main function to input radius of circle, create object and call the methods to print output.
import java.io.*; public class circle { static float PI; float radius; circle(float R) { radius = R; PI = 3.14F;
} public float FindArea( ) { float ar; ar = PI * (radius * radius); return ar; } public static void main( String args[]) throws IOException { BufferedReader inp= new BufferedReader(new InputStreamReader(System.in)); float r, area; System.out.println("Input data: "); System.out.print("Input value of the radius: "); r= Float.parseFloat(inp.readLine()); obj= new circle(r); area = obj.FindArea(); System.out.println("OUTPUT: "); System.out.print("Area of circle is = "+ area +" whose radius is "+r); } }
2 . Write a program to add the two different timings, the following method is used :
Time 1 = 10 hours 82 minutes
Time 2 = 7 hours 55 minutes
Sum of time 1 + time 2 = 19 hours 17 minutes(where 60 minutes = 1 hour)
Design a class Clock with the following details:
Instance variable/data members :
- h1, h2(integers to store time in hours)
- m1, m2(integers to store time in minutes)
Member functions/methods :
- public clock(int h, int m, int hh, int mm): constructor to initialize ‘h’ to ‘h1’, ‘hh’ to ‘h2’, ‘m’ to ‘ml’, and ‘mm’ to ‘m2’.
- public void showTime(): to display the two timings in hours and minutes (i.e. values of hl’, ‘ml’, ‘h2’, ‘m2’).
- public void addTime( ): to add values of ‘hl’, ‘m1, ‘h2’, ‘m2’ using the method given above and print new time.
Also, Design another class Time with main() function to create suitable object and print the data by invoking the above methods / functions.
public class clock { static int h1, h2; static int m1, m2; clock(int h, int m, int hh, int mm) { h1 = h; h2 = hh; m1 = m; m2 = mm; } public void showTime() { System.out.println("Output:"); System.out.println("TIME 1: "+h1+" Hours "+m1+" Minutes"); System.out.println("TIME 2: "+h2+" Hours "+m2+" Minutes"); } public void addTime() { int hour = h1+ h2; int minute = m1 + m2; if ( minute>=60) { hour = hour + (minute / 60); minute = minute % 60; } System.out.println("SUM OF THE TIME = "+ hour +" Hours and"+ minute +" Minutes"); } } public class Time { public static void main( ) { clock OBJ=new clock( 17, 86, 10, 61); OBJ. showTime(); OBJ .addTime(); } }
3 . A class Employee as given:
Class name: Employee Data members/variables : name (String type data) basic, Da, HRA, Total, pf, netsalary (all double type) Member functions/methods : (i) Employee(): A constructor to store name of employee (name) and basic salary (basic) of your choice. (ii) void calculation(): to calculate allowances and salaries as per the given criteria :
Da = 45% of basic
HRA= 15% of basic
pf = 8.33% of basic
Total = basic + Da + HRA
netsalary = Total – pf(iii) void print(): to display name and other data members with messages.
Write a main function to create an object of the class and print the details the employee by invoking the methods.
import java.io.*; public class Employee { String name; double basic, Da, HRA, Total, pf, netsalary; Employee( ) { name = "Mr. abc"; basic = 11600.0; } public void calculation( ) { Da = (basic * 45.0)/100.0; HRA = (basic * 15.0)/100.0; pf = (basic * 8.33)/100.0%3; Total = basic + Da + HRA; netsalary = Total - pf; } public void print() { System.out.println("Name of employee:"+ name); System.out.println("Basic salary: "+ basic); System.out.println("Dearness allowance: "+ Da); System.out.println("House rent allowance:"+ HRA); System.out.println("Provident fund:"+ pf); System.out.println("Gross salary: "+ Total); System.out.println("Net salary:"+ netsalary); } public static void main( String args[ ]) throws IOExcept { Employee obj = new Employee( ); obj. calculation(); System.out.println("OUTPUT: "); obj . print(); } }
4 . Design a class Sample1 with an integer data (a), one floating data(b)and one character (ch) as data members of the class. The functions of the class are as follows:
- Sample1(): a default constructor to assign values of your choice to data members ‘b’ and ‘ch’.
- void accept( ): to assign an integer value of your choice to data member ‘a’.
- void Show(): to print values stored in all the data members.
write a main function to create the object of class and call the methods to print the results.
public class Sample1 { char ch; int j; float b; Sample1() { ch = 'G'; b = 86; } public void accept() { j = 40; } public void Show() { System.out.println("The floating value in b = "+b); System.out.println("Integer value in a = "+j ); System.out.println("Character value in ch = "+ch ); } public void main( ) { Sample1 obj= new Sample1( ); System.out.println("OUTPUT 1: "); obj.Show( ); obj.accept( ); System.out.println("OUTPUT 2: "); obj.Show( ); } }
5 . Design a class Sample2 with an integer data (a), a floating data (b) and a character (ch) as data members of the class. The member functions of class are as follows :
- Sample2( float X, char C): a parameterized constructor to Assign values of ‘X’ to ‘b’ and ‘C’ to “ch’.
- void accept( ): to assign an integer value of your choice to data member ‘a’.
- void Show( ): to print values stored in all the data members.
Write a main function to create object of Sample2 type and call the methods accordingly, to print the output.
public class Sample2 { int a; float b; char ch; Sample2( float X, char E) { ch = E; b = X; } public void accept() { a=80; } public void Show() { System.out.println("The floating value in b = "+b); System.out.println("Integer value in a = "+a ); System.out.println("Character value in ch = "+ch ); } public void main() { Sample2 obj = new Sample2( 30, 'D' ); System.out.println("OUTPUT 1: "); obj . Show(); obj . accept(); System.out.println("OUTPUT 2: "); obj . Show(); } }
6 . Create a class Sample3 with an integer variable (a), floating variables (y, z) as data members of class. The member functions of the class are as follows :
- Sample3() : a default constructor to assign actual value of your choice to data member ‘a’.
- Sample3(float q ): a parameterized constructor to assign value of argument ‘q’ to y.
- void accept( ) : to assign an integer value of your choice to data member ‘z’.
- void Show() : to print actual values of ‘a’, ‘y’ and ‘z’.
Write a main function to create the object of Sample3 class. Print the values of variables ‘a’, ‘y’ and z’ in such a way that it could differentiate constructor and a normal function.
public class Sample3 { static int a; float y, z; Sample3( ) { a = 30; } Sample3( float q ) { y = q; } public void accept() { z=96; } public void Show() { System.out.println("Value of a = " + a ); System.out.println("Value of y = " + y); System.out.println("Value of z = " + z ); } public void main() { Sample3 obj = new Sample3(50); System.out.println("OUTPUT 1:"); obj . Show(); obj . accept(); System.out.println("OUTPUT 2:"); obj . Show( ); } }
7 . Example of constructor overloading using two parameterized constructors with one and two arguments.
Create a class Sample4 with an integer variable (b), floating variables (y, z) and a character variable (ch) as data members of class.
The functions of class are:
- Sample4 ( float q) : a parameterized constructor to assign argument ‘q’ to ‘y’.
- Sample4 ( int n, char C ): a parameterized constructor to assign value of arguments ‘n’ to ‘b’ and ‘C’ to ‘ch’.
- void accept( ) : to assign an integer value of your choice to the data member ‘z’.
- void Show1() : to print actual values of ‘y’ and ‘z’.
- void Show2() : to print actual values of ‘z’, ‘b’ and ‘ch’.
Write a main function to create the object of class Sample4 and print the results by invoking functions.
public class Sample4 { int b; float y, z; char ch; Sample4( float q ) { y=q; } Sample4( int n, char C) { b=n; ch= C; } public void accept( ) { z = 76; } public void Show1() { System.out.println("Value of y =" + y ); System.out.println("Value of z = " + z ); } public void Show2( ) { System.out.println("Value of b ="+b ); System.out.println("Valuc of z =" + z ); System.out.println("Value of ch = " + ch ); } public static void main() { Sample4 obj1 = new Sample4( 46 ); Sample4 obj2 = new Sample4( 55, 'M' ); System.out.println("Results of object obj1:"); System.out.println("OUTPUT 1: "); obj1.Show1( ); obj1.accept( ); System.out.println("OUTPUT 2: "); obj1.Show1( ); System.out.println("Results of object obj2:"); System.out.println("OUTPUT 1: "); obj2.Show2( ); obj2.accept( ); System.out.println("OUTPUT 2: "); obj2.Show2( ); } }
8 . Create a class stock as given :
Class name : | stock |
Data members/variables: | code (long integer type)- to store the product code qty (integer), price, total, discount, netPrice (all double type) |
Member functions/methods: | |
(i) stock(long n, int q, double p): | A constructor to assign ‘n’ to ‘code’, ‘q’ to qty’, ‘p’ to price. |
(ii) void Compute( ): | to calculate total cost, discount as 25% on total cost if total cost is more than 15000/- otherwise no discount. Also find net price to be paid excluding discount. |
(iii) void Display() : | to display product code, price, quantity, total cost, discount and net price to be paid. |
Write a main function to create suitable object of the class and print the details of the product by calling the suitable methods.
import java.io.*; public class stock { long code; int qty; double price; double total, discount, netPrice; stock(long n, int q, double p) { code = n; qty = q; price = p; } public void Compute() { total = price * qty; if(total > 15000.0) discount = (total *25.0)/100.0; else discount = 0.0; netPrice = total - discount; } public void Display() { System.out.println("Product Code: "+ code); System.out.println("Unit price: "+ price); System.out.println("Quantity taken: "+ qty); System.out.println("Total cost"+ total); System.out.println("Discount given"+ discount); System.out.println("Net price to be paid"+ netPrice); } public static void main( String args[ ]) throws IOException { stock obj = new stock(625321, 19, 15000.0 ); obj . Compute( ); System.out.println("OUTPUT: "); obj . Display( ); } }
9 . Specify a class Test with integers ‘v1’, ‘v2’. Assign 100 to v1 and 200 to v2 along with functions as:
- void show() : to declare variables vl, v2 and assign 300 to vl and 400 to v2. Print all the values of variables v1, v2 i.e. 100,200, 300 and 400.
- void display( int x, int y ): to assign ‘x’ to global v1 and ‘y’ to global v2 and print.
Write a main function that creates object of class and invokes function show( ). Also make the suitable use of keyword “this” to invoke function display( ) by passing suitable parameters and print values of v1, v2.
public class test { int v1 = 100, v2 = 200; public void show() { int v1 = 300, v2 = 400; System.out.println("Here values of local variables v1 and v2 are printed : "); System.out.println("v1 = " + v1 ); System.out.println("v2 = "+ v2 ); System.out.println("Here values of global variables v1 and v2 are printed using this keyword : "); System.out.println("this . v1 = "+ this.v1 ); System.out.println("this. v2 = "+ this.v2 ); } public void display( int x, int y) { this. v1 = x; this. v2 = y; System.out.println("Global v1 = " + v1 ); System.out.println("Global v2 ="+ v2 ); } public void main() { test b1 = new test(); b1. show( ); System.out.println("Now the results of function display() which is invoked by passing 500, 600:"); this.display(500, 600); } }
10 . Specify a class Test2 with two integers ‘v1’, ‘v2’ and following functions:
- void accept( int a, int b)- to assign ‘a’ to ‘v1’ and ‘b’ to “V2
- void display( ) – to print actual values of ‘v1’ and ‘v2’ by using this keyword.
Write a main function that creates two objects of the class and invokes above functions by passing suitable parameters and print actual values of data members using the two objects.
public class test2 { int v1, v2; public void accept( int a, int b) { v1 = a; v2 = b; } public void display() { System.out.println("v1 = " + this. v1 ); System.out.println("v2 = " + this. v2 ); } public void main() { test2 b1 = new test2(); test2 b2 = new test2( ); System.out.println("The following is the result of values stored in the object b1:"); b1. accept(10,20); b1.display(); System.out.println("The following is the result of values stored in the object b2:"); b1.accept(30,40); b1.display(); } }