-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathInheritanceInJava.java
More file actions
137 lines (110 loc) · 3.31 KB
/
InheritanceInJava.java
File metadata and controls
137 lines (110 loc) · 3.31 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import java.time.LocalDate;
import java.util.Scanner;
class Person {
String name, address;
int d, m, y;
double height, weight;
void setName(String name) {
this.name = name;
}
void setAddress(String address) {
this.address = address;
}
void setDate(int d, int m, int y) {
this.d = d;
this.m = m;
this.y = y;
}
void setHeight(double height) {
this.height = height;
}
void setWeight(double weight) {
this.weight = weight;
}
String getName() {
return name;
}
String getAddress() {
return address;
}
String getDate() {
String str = String.valueOf(d) + "/" + String.valueOf(m) + "/" + String.valueOf(y);
return str;
}
double getHeight() {
return height;
}
double getWeight() {
return weight;
}
int calculateAge() {
LocalDate myObj = LocalDate.now();
int age = myObj.getYear() - y;
return age;
}
}
class Student extends Person {
int roll_no, size;
void setPersonAttribute(String name, String address, int d, int m, int y, double height, double weight) {
super.setName(name);
super.setAddress(address);
super.setDate(d, m, y);
super.setHeight(height);
super.setWeight(weight);
}
void setsize(int size) {
this.size = size;
}
/*
* double avgmarks;
* void setMarks(double marks,int i) {
* this.marks[i] = marks;
* }
*/
void calculateAvg() {
Scanner sc1 = new Scanner(System.in);
System.out.println("Enter the size");
size = sc1.nextInt();
double marks[] = new double[size];
System.out.print("Enter marks of subject ");
for (int i = 0; i < size; i++) {
marks[i] = sc1.nextDouble();
// s1.setMarks(marks, i);
}
double total = 0;
for (int i = 0; i < size; i++) {
total = total + marks[i];
}
double avgmarks = total / size;
System.out.println("Average=" + avgmarks);
}
void display() {
System.out.print("Name : " + super.getName() + "\nAddress : " + super.getAddress() + "\nBirthdate : "
+ super.getDate() + "\nHeight : " + super.getHeight() + "\nWeight : " + super.getWeight() + "\nAge : "
+ super.calculateAge());
}
}
class InheritanceTest1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Student s1 = new Student();
System.out.print("Enter Name of the Student : ");
String name = sc.nextLine();
System.out.print("Enter Address of the Student : ");
String address = sc.nextLine();
System.out.print("Enter Birthday of the student (Date Month Year) : ");
int d = sc.nextInt();
int m = sc.nextInt();
int y = sc.nextInt();
System.out.print("Enter the Height of student in meters : ");
double height = sc.nextDouble();
System.out.print("Enter the Weight of student in Kg : ");
double weight = sc.nextDouble();
s1.setPersonAttribute(name, address, d, m, y, height, weight);
// s1.setsize(size);
// s1.setMarks(marks);
s1.calculateAvg();
s1.display();
sc.close();
}
}