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

Assignment 3

The document defines classes to represent students in a class roster. It creates subclasses of Student for Undergraduate and Graduate students. The Student class stores name and test scores, while the subclasses override the getCourseGrade method to calculate grades differently based on undergraduate or graduate status. Main creates sample Undergraduate and Graduate objects, sets their attributes, and prints the output.

Uploaded by

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

Assignment 3

The document defines classes to represent students in a class roster. It creates subclasses of Student for Undergraduate and Graduate students. The Student class stores name and test scores, while the subclasses override the getCourseGrade method to calculate grades differently based on undergraduate or graduate status. Main creates sample Undergraduate and Graduate objects, sets their attributes, and prints the output.

Uploaded by

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

Name: Rao Humza

Enrollment:02-132212-010

Task 1

Code

package com.mycompany.assignment3;

public class Assignment3 {

public static void main(String[] args) {


HourlyWorker h= new HourlyWorker("Humza", 10);

System.out.println(h);

MonthlyWorker m= new MonthlyWorker("Majeed", 20000);

System.out.println(m);

class Worker

private String name;

public String getname()

return name;

public void setname(String name)

this.name=name;

public Worker(String name)

this.name=name;

public String toString()

{
return "Name: " + name;

class HourlyWorker extends Worker

private int hour;

public HourlyWorker(String name, int ho)

super (name);

hour= ho;

public int earning()

return hour*5;

public String toString()

return super.toString()+"\nHours worked: "+hour+"\nEarning: "+earning();

class MonthlyWorker extends Worker

private double salary;

public MonthlyWorker(String name, double sa)

{
super(name);

salary=sa;

public double earning()

return salary;

public String toString()

return super.toString()+ " \nEarning: "+earning();

Output
Task 2
Suppose we want to implement a class roster that contains both undergraduate and graduate students.

Each student’s record will contain his or her name, three test scores, and the final course grade.

The formula for determining the course grade is different for graduate students than for undergraduate
students.

Code

package com.mycompany.assignment3;

public class Task2 {

public static void main(String[] args)

Undergraduate u= new Undergraduate();

u.setName("Humza");

u.setTestScore(1, 100);

u.setTestScore(2, 80);

u.setTestScore(3, 90);

System.out.println("Undergraduate Student");
System.out.println("Name: "+ u.getName());

System.out.println("Grade: "+ u.getCourseGrade());

System.out.println("Score of test 1: " + u.getTestScore(1));

System.out.println("Score of test 2: " + u.getTestScore(2));

System.out.println("Score of test 3: " + u.getTestScore(3));

Graduate g= new Graduate();

g.setName("Arooba");

g.setTestScore(1, 80);

g.setTestScore(2, 80);

g.setTestScore(3, 70);

System.out.println("Graduate Student");

System.out.println("Name: "+ g.getName());

System.out.println("Grade: "+ g.getCourseGrade());

System.out.println("Score of test 1: " +g.getTestScore(1));

System.out.println("Score of test 2: " +g.getTestScore(2));

System.out.println("Score of test 3: " +g.getTestScore(3));

class Student

public final static int NUM_OF_TESTS = 3;


protected String name;

protected int[] test;

protected String courseGrade;

public Student( )

this("Welcome");

public Student(String studentName)

name = studentName;

test = new int[NUM_OF_TESTS];

courseGrade = "****";

public String getCourseGrade( ) {

return courseGrade;

public String getName( ) {

return name;

public int getTestScore(int testNumber) {

return test[testNumber-1];

public void setName(String newName) {

name = newName;

}
public void setTestScore(int testNumber,

int testScore) {

test[testNumber-1] = testScore;

class Undergraduate extends Student

@Override

public String getCourseGrade()

int total=0;

for(int i=0; i<NUM_OF_TESTS; i++)

total+=test[i];

if(total/NUM_OF_TESTS>=70)

courseGrade= "Pass";

else

courseGrade="Fail";

}
return courseGrade;

class Graduate extends Student

@Override

public String getCourseGrade()

int total=0;

for(int i=0; i<NUM_OF_TESTS; i++)

total+=test[i];

if(total/NUM_OF_TESTS>=80)

courseGrade= "Pass";

else

courseGrade="Fail";

return courseGrade;

}
Output

You might also like