100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Class notes

Java inheritance Example

Rating
-
Sold
-
Pages
10
Uploaded on
06-05-2022
Written in
2021/2022

This is a document that will assist students in the ICT and Computer Science Department.

Institution
Course









Whoops! We can’t load your doc right now. Try again or contact support.

Written for

Course

Document information

Uploaded on
May 6, 2022
Number of pages
10
Written in
2021/2022
Type
Class notes
Professor(s)
Justus ngare
Contains
College and university

Subjects

Content preview

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();
}

}
$7.49
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
Mnyazi

Get to know the seller

Seller avatar
Mnyazi Freelancer
Follow You need to be logged in order to follow users or courses
Sold
0
Member since
3 year
Number of followers
0
Documents
119
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions