|
First, the contents of the experiment:
Topic 1
Topic 1: Assuming that the student's 3 degree course scores determine whether or not they can get a degree, for undergraduates, if the average score of 3 courses is more than 60 points, and for graduate students, the average of more than 80 points to be able to pass. In accordance with the above requirements, please complete the following Java Class Design: (1) design a base class student describe the common characteristics of students. (2) Design a class undergraduate describing undergraduates, which inherits and extends the student class. (3) Designing a class graduate that describes graduate students, which inherits and extends the student class. (4) The design of a test class Studentdemo, respectively, to create the undergraduate and postgraduate two classes of objects, and output relevant information. Write a Java application program, create a student class, undergraduate class, Graduate class, the specific reference code is as follows: (fill in the Missing Code section)
Package PDSU; Define a PDSU package name
Class student{
private String name;
private int classa,classb,classc;
Public Student (String name,int classa,int classb,int ClassC) {
this.name=name;//Assignment Initial value
This.classa=classa;
THIS.CLASSB=CLASSB;
THIS.CLASSC=CLASSC;
}
Public String GetName () {
return name;
}
public int getaverage () {
Return (CLASSA+CLASSB+CLASSC)/3;
}
}
Class Undergraduate extends student{//defines undergraduate as a subclass of Student
Public undergraduate (String name,int classa,int classb,int ClassC) {
Super (NAME,CLASSA,CLASSB,CLASSC);
}
public void Ispass () {
if (Super.getaverage () >=60)//Find undergraduate students of the average score >=60
SYSTEM.OUT.PRINTLN ("undergraduate" +getname () + "three subjects are divided evenly:" +getaverage () + ", you can get a bachelor's degree. ");
Else
SYSTEM.OUT.PRINTLN ("undergraduate" +getname () + "of the three subjects is divided evenly:" +getaverage () + ", can not get a bachelor's degree. ");
}
}
Class Graduate extends student{
Public graduate (String name,int classa,int classb,int ClassC) {
Super (NAME,CLASSA,CLASSB,CLASSC);
}
public void Ispass () {
if (Getaverage () >=80)
System.out.println ("Postgraduate name" +super.getname () + "Average score" +super.getaverage ());//will be able to get a bachelor's degree in the name of the graduate, the average output of the three subjects.
Else
System.out.println ("Graduate" +getname () + "The three subjects are divided evenly:" +getaverage () + ", can not get a master's degree. ");
}
}
public class studentdemo{
public static void Main (string[] args) {
Undergraduate S1=new Undergraduate ("Tom", 55,75,81);
Graduate S2=new graduate ("Mary", 72,81,68);
S1.ispass ();//Find out the names of students who can get a bachelor's degree, and the average score of three subjects.
S2.ispass ();//Find out the names of students who can get a master's degree, and the average score of three subjects.
}
}
Operation Result:
The average undergraduate Tom's three subjects are divided into: 70, can get a bachelor's degree.
The three subjects of the Graduate Mary are divided into 73, which cannot get a master's degree.
Topic Two
Experiment Topic 2: Understanding the inheritance of classes. (1) Define a class rectangle that represents a rectangle, which contains two private double type member variables length and width respectively represent the length and width of the rectangle, define an argument to construct the method rectangle (double length, Double width), in which a rectangle object is initialized, defining a parameterless constructor, in which a constructor method with parameters is called, so that the object is created with a length and width of 0. Define the method perimeter () and the method area () for calculating the circumference of the rectangle. (2) Define a box class cuboid, which inherits the rectangle class, which contains a height of double type member variable representing high; Defines a constructor method cuboid (double length, double width, double height); Define a method to find the cuboid surface area (double height) and the method volume () to calculate the volume. (3) write an application called Cuboidtest.java to find the volume of a cuboid with a length, width and height of 10, 5, and 2 respectively.
Class rectangle{
private double length;
private double width;
Rectangle (double length,double width) {
This.length=length;
This.width=width;
}
Rectangle () {
This (0,0);
}
public void perimeter () {
System.out.println ("Perimeter is" +this.length*2+this.width*2);
}
public void area () {
System.out.println ("area is" +this.length*this.width);
}
}
Class Cuboid extends rectangle{
Double height;
Double length;
Double width;
Cuboid (double length,double width,double height) {
This.length=length;
This.width=width;
This.height=height;
}
public void area (double height) {
System.out.print ("surface area is" +this.length*this.width*4+this.width*height*2);
}
public void volume () {
System.out.println ("volume is" +this.length*this.width*this.height);
}
}
public class Cuboidtest {
public static void Main (string[] args) {
Cuboid C = new Cuboid (10,5, 2);
C.volume ();
}
}
Run results
Volume is 100.0
Topic Three
Experiment Topic 3: Students eat (1) build a character class and implement the Eat Method (2) Build the student class, which is final, not allowed to be inherited, inherit the character class and implement the Eat Method (3) Create a student class instance in main and call the Eat method
Class person{
public void Eat () {
}
}
Final class Student extends person{
}
public class Test {
public static void Main (string[] args) {
Student st = new Student ();
St.eat ();
}
}
|