-
Notifications
You must be signed in to change notification settings - Fork 0
/
chit13.java
48 lines (40 loc) · 1.45 KB
/
chit13.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//~Define a class student with four data members such as name, roll no, sub1 and sub2. Define appropriate methods to INITITALIZE AND DISPLAY VALUES OF DATA MEMBERS also calculate total marks and percentage scored by student in three subjects.
class Student {
// data members
private String name;
private int rollNo;
private double sub1;
private double sub2;
// Constructor to initialize data members
public Student(String name, int rollNo, double sub1, double sub2) {
this.name = name;
this.rollNo = rollNo;
this.sub1 = sub1;
this.sub2 = sub2;
}
// Method to display values of data members
public void display() {
System.out.println("Name: " + name);
System.out.println("Roll No: " + rollNo);
System.out.println("Marks in subject 1: " + sub1);
System.out.println("Marks in subject 2: " + sub2);
}
// Method to calculate total marks
public double totalMarks() {
return sub1 + sub2;
}
// Method to calculate percentage scored
public double percentage() {
return totalMarks() / 2;
}
}
public class chit13 {
public static void main(String[] args) {
Student s = new Student("Saiee", 1, 85.5, 90.0);
s.display();
double total = s.totalMarks();
double percentage = s.percentage();
System.out.println("Total marks: " + total);
System.out.println("Percentage: " + percentage);
}
}