You are on page 1of 7

Experiment - 4

Class OOPs

Type Lab

Name: Muditt Sharmma


Roll No.: R2142210506
SAP ID: 500091725
Branch: Btech. CSE spl (CSF)

AIM:
1. Write a JAVA program to implement class mechanism. – Create a class, define datamembers, methods
(setData(args) ,getData()) and invoke them inside main method.

2. Write software to prepare the results of the student for school examination. The Roll no ofthe students and their marks
in subjects of Physics, Chemistry and Maths are suppliedthrough the Scanner class. The result of the subject is
prepared and displayed on the outputscreen by finding the students who fail and pass the exam. If the marks are
below 40% insubject, then the student has failed in the subject. If the student passes and the aggregatescore is above
75% then he scores Distinction. If the student passes and aggregate marksare 60%,50%, and 40% then he scores
First, Second, and Third Division. Mention all thecases in output.

3. Write a JAVA program to implement compile time polymorphism.

4. Write a JAVA program to implement type promotion in method overloading.

5. Write a JAVA program to implement constructor overloading.

1) Write a JAVA program to implement class mechanism. – Create a class,


define datamembers, methods (setData(args) ,getData()) and invoke them
inside main method.

import java.util.*;
class Sleep{
static String Sleptat;
static String Wokeupat;
static Scanner input;

public static void main(String args[])


{
System.out.println("Name: Muditt Sharmma,\n Sap ID : 500091725, \n Roll no. : R2142210506");
input = new Scanner(System.in);
setinfo();
getinfo();
}

public static void setinfo(){


System.out.println("Slept at:");
Sleptat=input.next();
System.out.println("Woke up at:");
Wokeupat=input.next();
}

public static void getinfo(){


System.out.println("Slept From:"+Sleptat);
System.out.println("Till:"+Wokeupat);
}
}

Experiment - 4 1
Algorithm:
1. Import all the modules from java util library.

2. create a class Sleep

3. create two string variables, Sleptat, Wokeupat.

4. create a Scanner input.

5. create the main method.

6. create a new instance for scanner using new.

7. call the setinfo() and getinfo() methods in it.

8. create the setinfo() method.

9. take input from the user.

10. create the getinfo() method.

11. show the input taken from the user.

2) Write software to prepare the results of the student for school


examination. The Roll no ofthe students and their marks in subjects of
Physics, Chemistry and Maths are suppliedthrough the Scanner class. The
result of the subject is prepared and displayed on the outputscreen by
finding the students who fail and pass the exam. If the marks are below 40%
insubject, then the student has failed in the subject. If the student passes
and the aggregatescore is above 75% then he scores Distinction. If the
student passes and aggregate marksare 60%,50%, and 40% then he scores
First, Second, and Third Division. Mention all thecases in output.

import java.util.*;

class StudentMarks{
static String Name;
static int Rollno;
static String Branch;
static Scanner input;
static int PhysicsPer;
static int ChemistryPer;
static int MathsPer;
static int Aggregate;

Experiment - 4 2
public static void main(String[] args){
System.out.println("Name: Muditt Sharmma,\n Sap ID : 500091725, \n Roll no. : R2142210506");
input = new Scanner(System.in);
setinfo();
getinfo();
sorted();
}

public static void setinfo(){


System.out.println("Name:");
Name=input.next();
System.out.println("Roll no:");
Rollno=input.nextInt();
System.out.println("Branch:");
Branch=input.next();
System.out.println("Physics Percentage:");
PhysicsPer=input.nextInt();
System.out.println("Chemistry Percentage:");
ChemistryPer=input.nextInt();
System.out.println("Maths Percentage:");
MathsPer=input.nextInt();
System.out.println("");
}

public static void getinfo(){


System.out.println("Student Details:");
System.out.println("Name:"+Name);
System.out.println("Roll no:"+Rollno);
System.out.println("Branch:"+Branch);
System.out.println("");
}

public static void sorted(){


System.out.println("Physics Percentage:"+PhysicsPer);
if(PhysicsPer<100 && PhysicsPer<40){
System.out.println("You failed in physics");
}
else if(PhysicsPer<100 && PhysicsPer>75){
System.out.println("You scored a distinction in physics");
}
else if(PhysicsPer<100 && PhysicsPer>=40){
System.out.println("You passed");
}
else{
System.out.println("INVALID");
}
System.out.println("");

System.out.println("Chemistry Percentage:"+ChemistryPer);
if(ChemistryPer<100 && ChemistryPer<40){
System.out.println("You failed in Chemistry");
}
else if(ChemistryPer<100 && ChemistryPer>75){
System.out.println("You scored a distinction in Chemistry");
}
else if(ChemistryPer<100&& ChemistryPer>=40){
System.out.println("You passed");
}
else{
System.out.println("INVALID");
}
System.out.println("");

System.out.println("Maths Percentage:"+MathsPer);
if(MathsPer<100 && MathsPer<40){
System.out.println("You failed in Maths");
}
else if(MathsPer<100 && MathsPer>75){
System.out.println("You scored a distinction in Chemistry");
}
else if(MathsPer<100&& MathsPer>=40){
System.out.println("You passed");
}
else{
System.out.println("INVALID");
}
System.out.println("");

Aggregate=(PhysicsPer+MathsPer+ChemistryPer)/3;
System.out.println("Aggregate:"+Aggregate);
if(Aggregate<100 && Aggregate<40){
System.out.println("You have failed. You have to repeat a year");
}
else if(Aggregate<100 && Aggregate>75){
System.out.println("You have scored a distinction");
}
else if(Aggregate>=40 && Aggregate<50){
System.out.println("You passed Third Division");
}
else if(Aggregate>=50 && Aggregate<60){

Experiment - 4 3
System.out.println("You passed Second Division");
}
else if(Aggregate>=60 && Aggregate<75){
System.out.println("You passed First Division");
}
else{
System.out.println("INVALID");
}
}

Algorithm:
1. Import every module from java util library

2. create a class StudentMarks

3. create the required variables like Name, Roll no, Branch, and marks.

4. create the scanner input

5. create the main method

6. create a new instance for scanner using new

7. call the setinfo(), getinfo() and sorted() methods in it.

8. create the setinfo() method.

9. take input from the user

10. create the getinfo() method

11. show the user input

12. create the sorted method

13. check the conditions for passing, distinction, first division, second division and third division and show the result
accordingly

3) Write a JAVA program to implement compile time polymorphism.

public class Polymorphism {


static void sum(int a, int b)
{
System.out.println("Sum of the integers: "+(a+b));
}
static void sum(double a, double b)

Experiment - 4 4
{
System.out.println("Sum of the doubles: "+(a+b));
}

public static void main(String[] args)


{
sum(1, 2);

sum(1.2, 2.4);
}
}

Algorithm:
1. create a class called Polymorphism

2. create sum function with parameters int a and int b

3. print the sum of integers a and b

4. create sum functions with parameters double a and double b

5. print the sum of double a and double b

4)Write a JAVA program to implement type promotion in method overloading.

public class TypePromotion{


static double sum(int num1, int num2) {
double s = 0;
s = num1 + num2;
return s;
}

static double sum(int num1, double num2) {


double s = 0;
s = num1 + num2;
return s;
}

public static void main(String[] args) {


double result = 0;

result = sum(10, 20);


System.out.println("Sum : " + result);

result = sum(20, 20.56F);


System.out.println("Sum : " + result);
}
}

Experiment - 4 5
Algorithm:
1. Create a class TypePromotion

2. Create a sum function with parameters as int num1 and int num2

3. create a double variable s with initial value 0

4. let s = num1 + num2

5. return s

6. Create main method

7. create a double variable result with initial value 0

8. result = sum(10,20)

9. show result

10. result = sum(20,20.56F)

11. show result

5) Write a JAVA program to implement constructor overloading.

class ConstructorOverloading
{
private int Rupee;
ConstructorOverloading()
{
Rupee =49;
}
ConstructorOverloading(int r)
{
this();
Rupee = Rupee+ r;

}
public int getRupee(){
return Rupee;
}
public void setRupee(int Rupee) {
this.Rupee = Rupee;
}
public static void main(String args[])
{
ConstructorOverloading obj = new ConstructorOverloading(12);
System.out.println(obj.getRupee());
}
}

Experiment - 4 6
Algorithm:
1. create a class ConstructorOverloading

2. create a Rupee method

3. create a private integer variable Rupee

4. create a default constructor ConstructorOverloading()

5. set Rupee to 49

6. create a parameterized constructor ConstructorOverloading with parameters int r

7. call the default constructor from parameterised constructor using this()

8. set Rupee=Rupee+r

9. create the getRupee() method

10. return Rupee

11. create the setRupee() method

12. set this.Rupee as Rupee

13. create the main method

14. create obj as the new instance for ConstructorOverloading() and pass 12 as the value of r in the parenthesis

15. show the final Rupee value

Experiment - 4 7

You might also like