0% found this document useful (0 votes)
50 views

Q.1 Write A Java Program To Demonstrate The Use of Inheritance. Algorithm

This document contains code for a Java program that demonstrates inheritance and storing student test marks using classes and methods. It includes: 1) A TestDetails class with a 2D array to store student marks and methods to store and display marks. 2) A NoticePeriod class that inherits from TestDetails and has a method to count the number of students who took less than 3 tests or scored below 50 in at least 3 tests. 3) A main method that creates objects, calls the storage and display methods, and calls the NoticePeriod method to output students on notice.

Uploaded by

Shistata Subedi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Q.1 Write A Java Program To Demonstrate The Use of Inheritance. Algorithm

This document contains code for a Java program that demonstrates inheritance and storing student test marks using classes and methods. It includes: 1) A TestDetails class with a 2D array to store student marks and methods to store and display marks. 2) A NoticePeriod class that inherits from TestDetails and has a method to count the number of students who took less than 3 tests or scored below 50 in at least 3 tests. 3) A main method that creates objects, calls the storage and display methods, and calls the NoticePeriod method to output students on notice.

Uploaded by

Shistata Subedi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

CSE1007 Java Programming LAB ASSESMENT-3

SHISTATA SUBEDI 19BCE2653

Q.1 Write a Java program to demonstrate the use of Inheritance.

ALGORITHM:
1. Create a class ‘Laptop’ which contains a method ‘display()’ which displays “Working..”.
2. Create a subclass of class ‘Laptop’ named ‘Dell’ which contains a method ‘print()’ which
displays “Dell Inspiron”.
3. Create a subclass of ‘Dell’ named ‘Accessories’ which contains a method ‘presents()’ which
displays “Wireless Mouse”.
4. Create a subclass of class ‘Laptop’ named ‘Lenovo’ which contains a method ‘show()’ which
displays “Lenovo YOGA
5. In the main class named ‘LaptopFunc’, make an instance or object ‘d’ of the class “Laptop”,
“Dell”, “Accessories” and call method ‘display ()’ from the same class and ‘present()’, ‘print()’.
CODE:
class Laptop
{
void display()
{
System.out.println("Working....");
}
}
class Dell extends Laptop
{
void print()
{
System.out.println("Dell Inspiron");
}
}
class Accessories extends Dell
{
void presents()
{
System.out.println("Wireless Mouse");
}
}
class Lenovo extends Laptop
{
void show()
{
System.out.println("Lenovo YOGA");
}
}
public class LaptopFunc
{
public static void main(String args[]){
Accessories d = new Accessories();
d.print();
d.presents();
d.display();
Lenovo l = new Lenovo();
l.show();
l.display();
}}
OUTPUT:

Q.2 A training center conducts a total of 7 tests for its students. Students are allowed to skip few
tests. Let there be 25 students in the batch. So in the main class for every student, read the
number of tests taken and the marks scored in each test. A class ‘TestDetails’ should be defined
with a 2D array of float types to store the marks of all the students. Define a method
‘storeMarks()’ that will receive the following details for every student from the main class and
create in the 2D array, as many columns as equal to the number of tests, so as to store the marks.
There is no need to store the number of tests. Define another method ‘displayMarks()’ to print
the details. Also, the training center wishes to keep those students in notice period who have
taken < 3 tests and those who have not scored ≥ 50 in at least 3 tests. Derive another class
‘NoticePeriod’ from ‘TestDetails’ that includes a method to count and print the number of
students on the bench. Also, it should print the ID of those students assuming that the row index
of the array to be their ID. While checking do not proceed to check the marks in all tests, if the
student has already scored more than 50 in 3 tests. Instantiate this class from the main class and
do the required processing.
ALGORITHM:
1. Create a class testDetails having members as a 2D float array named ‘marks’
having sizes row=25 and column=7.
2. Inside the class, define a method ‘storeMarks()’.
3. Start iteration for 25 times in integer i.
4. Initialize a variable ‘n’ as an integer.
5. Input the value of n.
6. Start the iteration for 7 times in j.
7. Input the values of marks[i][j] if j<n.
8. Else assign marks[i][j]=-1.
9. Define the method displayMarks().
10.Again using loops, display the marks of each of the students.
11.While displaying, if marks[i][j]<0, then break without displaying.
12.Create a class named ‘NoticePeriod’ which gets inherited from the class
‘TestDetails’.
13. Create a method named ‘notice()’.
14.Inside the method ‘notice()’:
i. Initialize integer count=0.
ii. Begin the iteration for 25 times in i.
iii. Initialize integer count1=0.
iv. Begin iteration for 7 times in j
a. If marks[i][j]>=50, then count1 = count1+1.
v. If count1<3, then display value of i(as id) and count=count+1.
vi. Display the value of count as the students.
This method works for both types of students(who have taken less than 3 tests
and who have not scored greater than or equal to 50 in at least 3 tests.)
because the students who have scored more than or equal to 50 in at least 3
tests must have given at least 3 tests.
15. Inside the main class, create an instance or object named as ‘nocp’ of the class NoticePeriod.
16. Call the method nocp.storeMarks().
17. Call the method nocp.displayMarks().
18. Call the method nocp.notice().
19. Finally, all the data will be displayed.
CODE:
import java.util.Scanner;
public class Main{
public static void main(String[] args)
{
NoticePeriod notice = new NoticePeriod();
float marks[][] = new float[25][];
Scanner scan = new Scanner(System.in);

// loop to get the number of marks for each student and marks for each student
for(int i=0;i<marks.length;i++)
{
System.out.print("Enter the number of marks for Student-"+i+" (max 7 marks): ");
int numMarks = scan.nextInt();
marks[i] = new float[numMarks];
for(int j=0;j<marks[i].length;j++)
{
System.out.print("Enter marks-"+(i+1)+" : ");
marks[i][j] = scan.nextFloat();
}
}
notice.storeMarks(marks);
notice.displayMarks();
System.out.println();
notice.printBench();
scan.close();
}
}
class TestDetails {
protected float marks[][]; // array to store marks of students
// constructor
public TestDetails()
{
marks=null;
}
// method to store the marks of the students
public void storeMarks(float inMarks[][])
{
marks = new float[inMarks.length][];

for(int i=0;i<inMarks.length;i++)
{
marks[i] = new float[inMarks[i].length];

for(int j=0;j<inMarks[i].length;j++)
marks[i][j] = inMarks[i][j];
}
}
// method to display the marks of the students
public void displayMarks()
{
for(int i=0;i<marks.length;i++)
{
System.out.print("\nStudent "+i+" : ");

for(int j=0;j<marks[i].length-1;j++)
System.out.print(marks[i][j]+", ");
System.out.print(marks[i][marks[i].length-1]);
}
System.out.println();
}
}
class NoticePeriod extends TestDetails
{
// method to print the ids of the students on the bench and print the number of students on bench
public void printBench()
{
System.out.print("Students ID who are on bench : ");
int benchStudents = 0;
int countAbove50;
// loop to calculate the number of students on bench
for(int i=0;i<marks.length;i++)
{
if(marks[i].length < 3)
{
benchStudents++;
System.out.print(i+" ");
}
else
{
countAbove50 = 0;
for(int j=0;j<marks[i].length;j++)
{
if(marks[i][j] >= 50)
countAbove50++;
if(countAbove50 >= 3)
break;
}
if(countAbove50 < 3)
{
System.out.print(i+" ");
benchStudents++;
}
}
}
System.out.println("\nTotal Students on bench : "+benchStudents);
}
}
OUTPUT:
THANK YOU!

You might also like