0% found this document useful (0 votes)
5 views11 pages

Object Oriented Programming 2

The document contains a Java class named 'Student' with two constructors for initializing student details and methods for calculating grades based on one or three subjects. The class demonstrates method overloading by providing different ways to calculate grades and prints the results for two student objects. The main method tests the functionality of the class by creating student instances and invoking the grade calculation methods.

Uploaded by

olamohammed2101
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)
5 views11 pages

Object Oriented Programming 2

The document contains a Java class named 'Student' with two constructors for initializing student details and methods for calculating grades based on one or three subjects. The class demonstrates method overloading by providing different ways to calculate grades and prints the results for two student objects. The main method tests the functionality of the class by creating student instances and invoking the grade calculation methods.

Uploaded by

olamohammed2101
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

19:50 AA4O• l

Student.java X
public class Student {
private String studentName;
private int studentID;

1/ Constructor 1:
public Student (String studentName, int studentID) {
this.studentName = studentName;
Ian

T
Home End ( ) { < V

Editor Files Console Terminal


19:50 AA4O• l

Student.java X
this.studentName = studentName;
this.studentID = studentID;

2 1/ Constructor 2: Takes only studentName and assigns a default value to st


3 public Student (String studentName) {
4 this.studentName = studentName;
5 this.studentID = 999;
Home End ( ) { <

Editor Files Console Terminal


19:50 AA4O•
Student.java X

1/ Method Overloading: Calculate grade based on one subject


public void calculateGrade(int grade) {
if (grade >= 90 && grade <= 100) {
System.out.println(studentName + (ID: + student ID + ") received an
} else if (grade >= 80) {
System.out.println(studentName + (ID: + studentID + ") received a B
} else if (grade >= 70) {
< Home End ( ) { V

Editor Files Console Terminal


19:50 AA4O•
Student.java X

ethod Over loading: Calculate grade based on one subject


ic void calculateGrade(int grade) {
if (grade >= 90 && grade <= 100) {
System.out.println(studentName + "(ID: "+ studentID + ") received an A.");
} else if (grade >= 80) {
System.out.println (studentName + "(ID: "+ student ID + ")received a B.");
} else if (grade >= 70) {
Home End ( ) { <

Editor Files Console Terminal


19:50 A4O•
Student.java X
System.out.println(studentName + (ID: + studentID + ") received an A.");
} else if (grade >= 80) {
System.out.println(studentName + (ID: "+ studentID + ") received a B.");
} else if (grade >= 70) {
System.out.println(studentName+ "(ID: "+ student ID + ") received a C.");
} else if (grade >= 60) {
System.out.println(studentName + "(ID: "+ studentID + ") received a D.");
} else {
Home End ( ) { V

Editor Files Console Terminal


19:50 AA4O• l

Student.java X

System.out.println(studentName + "+ student ID + ")received a C.");


(ID:
} else if (grade >= 60 ){
System.out.println (studentName + "(ID: "+ studentID + ") received a D.");
} else {
System.out.println(studentName + (ID: "+ studentID + ") received an F.");

< Home End ( ) { V

Editor Files Console Terminal


19:50 AA4O•
Student.java X

1/Method
Overloading: grade based on three subjects
public void calculateGrade(int grade1, int grade2, int grade3 ){
int average = (grade1 + grade2 + grade3) / 3;
calculateGrade(average); // Reuse the single parameter method

< Home End ( ) { V

Editor Files Console Terminal


19:50 AA4O•
Student.java X
X

public static void main(String[] args) {


1/ Create objects using both constructors
Student student1 = new Student("Alice", 123);
Student student2 = new Student("Bob");

1/ Test calculateGrade with one subject

Home End ( ) { < V

Editor Files Console Terminal


19:51O• l

Student.java X

1/ Test calculateGrade with one subject


student1.calculateGrade(85); /
Should print B
student2. calculateGrade(92); I/Should print A

1/ Test calculateGrade with three subjects


student1.calculateGrade(75,80, 90); // Should print B for average
student2.calculateGrade(60, 70, 80); // Should print C for average

Home End ( ) {

Editor Files Console Terminal


19:51 A4O• l

Student.java X
47 student2.calculateGrade(92); I/ Should print A
48
49 1/ Test calculateGrade with three subjects
50 student1.calculateGrade(75, 80, 90); // Should print B for average
51 student2.calculateGrade(60, 70, 80); // Should print C for average
52
53 }

< Home End ( ) { <

Editor Files Console Terminal


19:51O• l

= Console

Alice (ID: 123) received a B.


Bob (ID: 999) received an A.
Alice (ID: 123) received a B.
Bob (ID: 999) received a C.

Editor Files Console Terminal

You might also like