Java eclipse homework ghostwriting, doing eclipse experiments on behalf of

Java eclipse homework writing and doing eclipse experiments
"java programming" course experimental report
Experiment 4 Inheritance and polymorphism
1. Experiment purpose and requirements
1. Understand how to use the modifiers of classes and their members, and master the inheritance and overloading of methods and coverage.
2. Master the inheritance relationship and derivation methods of classes.
3. Master the concept and use of polymorphism.
4. Master the definition and use of interfaces.
Second, the experimental content
(1) class inheritance exercise
1. Further Understanding of Inheritance A
new class can be created from an existing class, retaining the member variables and methods of the existing class and modifying them as needed. New classes can also add new variables and methods. This phenomenon is called class inheritance.
When creating a new class, it is not necessary to write all member variables and member methods. All members of the inherited class can be referenced simply by declaring that the class inherits from a defined class. The inherited class is called the parent or superclass, and the new class is called the subclass.
Java provides a huge class library for developers to inherit and use. These classes are designed for public purposes, so it's rare to find a class that does exactly what you need. You have to design your own class that can handle the actual problem. If you design this class to only implement inheritance, it is no different from the parent class. Therefore, subclasses are usually extended by adding new properties and methods. This makes the subclass larger than the superclass, but more specific, representing a more specific set of objects. That's what inheritance means.
2. Write two program files KY4_1.java and KY4_2.java to implement class inheritance.
3. The code of KY4_1.java is as follows:
public class KY4_1
{
protected String xm; //Name
protected int xh; //Student ID
void setdata(String m,int h) //This method assigns initial values ​​to the name and student ID
{
xm =m;
xh = h;
}
public void print() //Output the student's name and student number
{
System.out.println(xm+", "+xh);
}
}
4. Compile KY4_1.java to generate the class file KY4_1.class. Note: Program KY4_2.class is not run for now.
5. Write a program file KY4_2.java. The program function is as follows: the subclass KY4_2 inherits the parent class KY4_1, which not only has the member variables xm (name) and xh (student number) of the parent class, but also defines new member variables xy (college) and xi (department). In the subclass KY4_2, the method print() of the parent class is rewritten, in this method not only the name and student number of the student, but also the college and department of the student are output. In addition, a main method main is also defined in the subclass KY4_2. First, create an object f of the parent class KY4_1 in the main method, set the name of f to your own name, the student ID to "12321", and call the print() method to output the name and student ID of the object f. Next, create an object s of subclass KY4_2 in the main method, set the name of s as "Guo Na", the student number as "12345", the college as "School of Economics and Management", the department as "Department of Information Management", and call The print() method outputs the name, student ID, college and department of the object s.
6. Compile and run the program KY4_2. java. Please write the source program of KY4_2 and the running result in the experiment report.
Note: The parent class KY4_1 and the subclass KY4_2 must be in the same folder (path).
Requirements: Write the source code and running results of the program on the experimental report.

(2) Master the use of upcast objects, and understand that upcast objects of different objects may call the same method to produce different behaviors (polymorphism).
The upcast object can access the member variables inherited or hidden by the subclass, and can also call the method inherited by the subclass or the instance method overridden by the subclass (this is equivalent to calling these methods on the subclass object.) If the subclass overrides After an instance method of the parent class is specified, the instance method called by the object's upcast object must be the instance method overridden by the subclass).
1. Write an abstract class named Geometry, which has an abstract method.
public double area();
2. Write several subclasses of Geometry, such as Circle subclass and Rect subclass.
3. Write the Student class, which defines a public double area(Geometry...p) method. The parameters of this method are variable parameters, that is, the number of parameters is uncertain, but the types are all Geometry. This method returns the sum of the areas calculated by the parameter.
4. Create a Student object in the main method of the main class MainClass, and let the object call
public double area(Geometry ... p) to calculate the sum of the areas of several rectangles and several circles.
Geometry.java
public abstract class Geometry{
public abstract double getArea( );
}

Rect.java
public class Rect extends Geometry {
double a,b;
Rect(double a,double b) {
this.a = a;
this.b = b;
}
[Code 1] //Rewrite the getArea() method and return Rectangle area
}
Circle.java
public class Circle extends Geometry {
double r;
Circle(double r) {
this.r = r;
}
[Code 2] //Rewrite the getArea() method to return the area of ​​the circle
}

Student.java
public class Student {
public double area(Geometry ...p) {
double sum=0;
for(int i=0;i<p.length;i++) {
sum=sum+p[i].getArea();
}
return sum;
}
}

MainClass.java
public class MainClass{
public static void main(String args[]) {
Student zhang = new Student();
double area =
zhang.area(new Rect(2,3),new Circle(5.2),new Circle( 12));
System.out.printf("The area sum of 2 circles and 1 rectangle:\n%10.3f",area);
}
}
Experiment content requirements: replace the [code] part with java program code, Become a complete program and write the source code and running results of the program on the experimental report.
(3) Interface-oriented programming
Interface callback is another embodiment of polymorphism. Interface callback means that a reference to an object created by a class using an interface can be assigned to the interface variable, then the interface variable can call the method in the interface implemented by the class. When the interface variable calls the method in the interface implemented by the class When the method is called, it is to notify the corresponding object to call the method of the interface. The so-called interface-oriented programming means that when designing an important class, the class is not oriented to a specific class, but to an interface. That is, the important data in the design class is the variables declared by the interface, not the objects declared by the concrete class.
The weather may appear in different states, requiring different interfaces to encapsulate the weather state.
The specific requirements are as follows:
? 1. Write the interface WeatherState, which has a method named void showState().
? 2. Write the Weather class, which has a variable state declared by WeatherState. In addition, the class has a show() method, in which the interface state is called back to the showState() method.
? 3. Write several classes that implement the WeatherState interface, which are responsible for describing various states of the weather.
? 4. Write the main class and carry out the weather forecast in the main class.
WeatherState.java
public interface WeatherState { //interface
public void showState();
}
Weather.java
public class Weather {
WeatherState state;
public void show() {
state.showState();
}
public void setState(WeatherState s) {
state = s;
}
}
WeatherForecast.java
public class WeatherForecast { //main class
public static void main(String args[]) {
Weather weatherBeijing =new Weather();
System.out.print("\nToday's day:");
weatherBeijing.setState(new CloudyDayState());
weatherBeijing.show();
System.out.print("\n今天夜间:");
weatherBeijing.setState(new LightRainState());
weatherBeijing.show();
System.out.print("转:");
weatherBeijing.setState(new HeavyRainState());
weatherBeijing.show();
System.out.print("\n明天白天:");
weatherBeijing.setState(new LightRainState());
weatherBeijing.show();
System.out.print("\n明天夜间:");
weatherBeijing.setState(new CloudyLittleState());
weatherBeijing.show();
}
}
CloudyLittleState.java
public class CloudyLittleState implements WeatherState {
public void showState() {
System.out.print("Slightly cloudy, sometimes sunny.");
}
}
CloudyDayState.java
public class CloudyDayState implements WeatherState {
[Code 1] //Override public void showState()
}
HeavyRainState.java
public class HeavyRainState implements WeatherState{
[Code 2] //Override public void showState()
}
LightRainState. java
public class LightRainState implements WeatherState {
[Code 3] //Override public void showState() method
}

Experiment content requirements: replace the [code] part with java program code. Write the source code and running results of the program on the experimental report.
3. Experimental equipment and environment
windows7 and above, install eclipse+JDK
4. Experimental process and results
5. Experiment summary
http://www.6daixie.com/contents/9/1337.html

 

Our field of direction: window programming, numerical algorithm, AI, artificial intelligence, financial statistics, econometric analysis, big data, network programming, WEB programming, communication programming, game programming, multimedia linux, plug-in programming program, API, image processing, embedded/MCU database programming, console process and thread, network security, assembly language hardware Programming software design engineering standards and regulations. The ghostwriting and ghostwriting programming languages ​​or tools include but are not limited to the following:

C/C++/C# ghostwriting

Java ghostwriting

IT ghostwriting

Python ghostwriting

Tutored programming assignments

Matlab ghostwriting

Haskell ghostwriting

Processing ghostwriting

Building a Linux environment

Rust ghostwriting

Data Structure Assginment

MIPS ghostwriting

Machine Learning homework ghostwriting

Oracle/SQL/PostgreSQL/Pig database ghostwriting/doing/coaching

web development, website development, website work

ASP.NET website development

Finance Insurance Statistics Statistics, Regression, Iteration

Prolog ghostwriting

Computer Computational method

 

Because professional, so trustworthy. If necessary, please add QQ: 99515681 or email: [email protected]

WeChat: codinghelp

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325074422&siteId=291194637