Object Oriented Programming: Java Inheritance Example
Student
Marks
Looking at the is a inheritance
import java.io.*;
import java.util.*;
//To capture the student marks
class Marks{
int cat1, cat2;
int exam;
Marks(){
cat1=(int)Math.ceil(Math.random()*20);
cat2=(int)Math.ceil(Math.random()*10);
exam=(int)Math.ceil(Math.random()*70);
}//End of constructor
public int getCat1(){
return cat1;
}//End of getcat1
public int getCat2(){
return cat2;
}//End of getcat2
public int getExam(){
return exam;
}//End of getexam
}//End of class marks
/ The Student class. No methods. Equivalent to a struct
in c++ class Student extends Marks{
int stID; String
fname; String
mname;
String lname;
String dob;
Marks []mk=new Marks[7];//AQll the seven subjects
}
//Discuss what each of the line in the program is doing
//The Driver Class
public class StudentTestEx2{
//Declaring data that is required later in the program
, static Scanner scan=new Scanner(System.in);
//Array of subject names
static String subjects[]={"OOP1","Operating
System","Networking","OOP2","INternet Technology","Java
Servelets","Database Administration"}; //Array of subject codes
static String
codes[]={"CMT001","CMT002","CMT007","CMT003","CMT004","CMT005","CMT006"};
//Array of students
static Student []st;
static int s;
//The main function
public static void main(String [] args){
System.out.println("Enter the number of students in the class");
s=scan.nextInt();
st=createClass(s);
//Using the menu to give the user flexibilty of what
to do menu();//Calling the recursive menu function
}//End of main
//A method that creates a class of students as an array and the
return it public static Student [] createClass(int x){
String
fnames[]={"Jane","John","Maggy","Joyce","Beth","Jude","James","Walter","Joseph","Teddy","Francis
","Brian"};
String
mnames[]={"Wangui","Mwatha","Wanjiru","Wangui","Maina","Maina","Akolo","Aug","Otieno","TH
uo","Mwangi","Kamau"};
String
lnames[]={"Kamau","Wangu","Thuo","Apr","Kahuho","Kingoo","Kariri","Ochieng","Ouko","Kamau"
,"Kariri","Mburu"};
String
dobs[]={"11/13/1994","11/10/1984","11/9/1974","11/13/2000","11/13/1989","11/13/1994","10/13/196
4","6/6/1994","17/3/1993","11/13/1992","3/6/1990","11/13/1963"};
Student []s=new Student[x];// A class is an array of
students for(int i=0; i<s.length; i++){
s[i]=new Student();
int stnum=101000+(int)Math.ceil(Math.random()*999);// Generating the number
randomly s[i].stID=stnum;
int rand=(int)Math.ceil(Math.random()*11);
s[i].fname=fnames[rand];
s[i].mname=mnames[rand];
s[i].lname=lnames[rand];
s[i].dob=dobs[rand];
for(int j=0; j<s[i].mk.length; j++){
s[i].mk[j]= new Marks();
}
}
Student
Marks
Looking at the is a inheritance
import java.io.*;
import java.util.*;
//To capture the student marks
class Marks{
int cat1, cat2;
int exam;
Marks(){
cat1=(int)Math.ceil(Math.random()*20);
cat2=(int)Math.ceil(Math.random()*10);
exam=(int)Math.ceil(Math.random()*70);
}//End of constructor
public int getCat1(){
return cat1;
}//End of getcat1
public int getCat2(){
return cat2;
}//End of getcat2
public int getExam(){
return exam;
}//End of getexam
}//End of class marks
/ The Student class. No methods. Equivalent to a struct
in c++ class Student extends Marks{
int stID; String
fname; String
mname;
String lname;
String dob;
Marks []mk=new Marks[7];//AQll the seven subjects
}
//Discuss what each of the line in the program is doing
//The Driver Class
public class StudentTestEx2{
//Declaring data that is required later in the program
, static Scanner scan=new Scanner(System.in);
//Array of subject names
static String subjects[]={"OOP1","Operating
System","Networking","OOP2","INternet Technology","Java
Servelets","Database Administration"}; //Array of subject codes
static String
codes[]={"CMT001","CMT002","CMT007","CMT003","CMT004","CMT005","CMT006"};
//Array of students
static Student []st;
static int s;
//The main function
public static void main(String [] args){
System.out.println("Enter the number of students in the class");
s=scan.nextInt();
st=createClass(s);
//Using the menu to give the user flexibilty of what
to do menu();//Calling the recursive menu function
}//End of main
//A method that creates a class of students as an array and the
return it public static Student [] createClass(int x){
String
fnames[]={"Jane","John","Maggy","Joyce","Beth","Jude","James","Walter","Joseph","Teddy","Francis
","Brian"};
String
mnames[]={"Wangui","Mwatha","Wanjiru","Wangui","Maina","Maina","Akolo","Aug","Otieno","TH
uo","Mwangi","Kamau"};
String
lnames[]={"Kamau","Wangu","Thuo","Apr","Kahuho","Kingoo","Kariri","Ochieng","Ouko","Kamau"
,"Kariri","Mburu"};
String
dobs[]={"11/13/1994","11/10/1984","11/9/1974","11/13/2000","11/13/1989","11/13/1994","10/13/196
4","6/6/1994","17/3/1993","11/13/1992","3/6/1990","11/13/1963"};
Student []s=new Student[x];// A class is an array of
students for(int i=0; i<s.length; i++){
s[i]=new Student();
int stnum=101000+(int)Math.ceil(Math.random()*999);// Generating the number
randomly s[i].stID=stnum;
int rand=(int)Math.ceil(Math.random()*11);
s[i].fname=fnames[rand];
s[i].mname=mnames[rand];
s[i].lname=lnames[rand];
s[i].dob=dobs[rand];
for(int j=0; j<s[i].mk.length; j++){
s[i].mk[j]= new Marks();
}
}