Java Eclipse job generation, Generation Eclipse experiment
The experiment report of Java Program Design course
Experimental four-inheritance and polymorphism
First, the purpose and requirements of the experiment
1. Learn how to use the modifiers of a class and its members to master the inheritance, overloading, and overwriting of methods.
2, master the inheritance of the class and derived methods.
3, master the concept of polymorphism and use.
4, master the definition and use of the interface.
Ii. contents of the experiment
(i) Classes of inheritance Exercises
1. Further understanding of the meaning of inheritance
The new class can be generated from existing classes and retains member variables and methods of existing classes and can be modified as needed. New classes can also add new variables and methods. This behavior is called inheritance of classes.
When you create a new class, you do not have to write out all the member variables and member methods. Simply declaring that the class inherits from a defined class can refer to all members of the inherited class. The inherited class is called the parent class or superclass (superclass), and the new class is called a subclass.
Java provides a large class library for developers to inherit and use. These classes are designed for common purposes, so there are very few classes that meet your needs. You have to design your own classes that can handle real-world problems, and if you design a class that only implements inheritance, it is no different from the parent class. Therefore, it is common to extend a subclass to add new properties and methods. This makes subclasses larger than the parent class, but more specific, representing a more concrete set of objects. The meaning of inheritance lies in this.
2. Write two program Files Ky4_1.java and Ky4_2.java to implement class inheritance.
3. The code for Ky4_1.java is as follows:
public class Ky4_1
{
protected String XM; Name
protected int xh; School Number
void SetData (String m,int h)//This method assigns an initial value to a name and a school number
{
XM =m;
XH = h;
}
public void print ()//output student's name and school number
{
System.out.println (xm+ "," +xh);
}
}
4. Compile Ky4_1.java, generate class file Ky4_1.class. Note: Do not run program Ky4_2.class temporarily.
5. Write a program file Ky4_2.java. The program functions as follows: Subclass Ky4_2 inherits the parent class Ky4_1, which not only has the member variables of the parent class XM (name), XH (school number), but also defines the new member variable XY (college), XI (department). The method print () of the parent class is overridden in subclass Ky4_2, in which not only the student's name and school number are exported, but also the student's faculties and faculties are exported. In addition, a primary method, main, is also defined in subclass Ky4_2. First, create a parent class Ky4_1 object F in the main method, set the name of F to be your own name, the number is "12321", and call the print () method to output the name and number of object F. Next, create a subclass Ky4_2 object s in the main method, set S name as "Guo Na", study number "12345", Academy for "School of Economics and Management", Department of "Information Administration", and call the print () method output object s name, school number, college and department.
6. Compile and run the program Ky4_2.java. Please write the source program of Ky4_2 and the result of running in the experiment report.
Note: The parent class Ky4_1 with the subclass Ky4_2 to be within the same folder (path).
Requirements: The source code of the program, the results of the operation is written on the experimental report.
(b) Mastering the use of the transformed object, understanding that the same method may produce different behavior (polymorphism) for different objects on the upper transformation object.
On a transformed object, you can access a member variable that is inherited or hidden by a subclass, or you can call an instance method overridden by a method or subclass of a subclass that is equivalent to a subclass object to invoke these methods. If the subclass overrides an instance method of the parent class, the instance method invoked on the object's top-transforming object must be an instance method of the subclass override.
1. Write an abstract class with the class name geometry, which has an abstract method.
Public double area ();
2. Write several subclasses of the geometry, such as the Circle subclass and the Rect subclass.
3. Write the student class, which defines a public double area (Geometry ... p) method, which is a variable parameter, that is, the number of arguments is indeterminate, but the type is Geometry. The method returns the sum of the area calculated by the parameter.
4. Create an student object in main class MainClass's main method to have the object call
Public double Area (Geometry ... p) calculates the sum of the areas of several rectangles and a number of 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"//Override Getarea () method, return rectangular area
}
Circle.java
public class Circle extends Geometry {
Double R;
Circle (Double R) {
THIS.R = R;
}
"Code 2"//Override Getarea () method, return Circle area
}
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 ("2 circles and a rectangular shape of the area and: \n%10.3f", areas);
}
}
Experimental content requirements: replace the "code" section with Java program code, become a complete program and the program's source code, running results are written on the experimental report.
(iii) interface-oriented programming
Interface callbacks are another manifestation of polymorphism. An interface callback is a method that allows a reference to an object created by a class that uses an interface to be assigned to that interface variable, so that the interface variable can invoke the method in the interface implemented by the class and, when the interface variable calls the method in the interface implemented by the class, notifies the corresponding object to invoke the interface. The so-called interface-oriented programming means that when designing an important class, the class is not oriented to the specific class, but the interface is oriented. That is, the important data in the design class is the variable that the interface declares, not the object declared by the concrete class.
Weather may appear in different states, requiring different interfaces to encapsulate the state of the weather.
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 invoked to Showstate ().
? 3. Write a number of classes that implement the Weatherstate interface, which is responsible for characterizing the various states of the weather.
? 4. Write the main class and make 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 ("\ n Today day:");
Weatherbeijing.setstate (New Cloudydaystate ());
Weatherbeijing.show ();
System.out.print ("\ n Today night:");
Weatherbeijing.setstate (New Lightrainstate ());
Weatherbeijing.show ();
System.out.print ("Turn:");
Weatherbeijing.setstate (New Heavyrainstate ());
Weatherbeijing.show ();
System.out.print ("\ n Tomorrow day:");
Weatherbeijing.setstate (New Lightrainstate ());
Weatherbeijing.show ();
System.out.print ("\ n tomorrow night:");
Weatherbeijing.setstate (New Cloudylittlestate ());
Weatherbeijing.show ();
}
}
Cloudylittlestate.java
public class Cloudylittlestate implements Weatherstate {
public void Showstate () {
System.out.print ("Less clouds, 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
}
Experimental content requirements: replace the "code" section with Java program code. Write the program's source code and running results on the experiment report.
Third, the experimental equipment and the environment
Windows7 and above system, installation ECLIPSE+JDK
Iv. Experimental process and results
V. Summary of the Experiment
Http://www.6daixie.com/contents/9/1337.html
Our Direction field: Window Programming numerical algorithm AI Artificial Intelligence financial statistical Metrology analysis Big Data network programming Web programming Communication Programming game Programming Multimedia Linux plug-in programming API image processing embedded/Microcontroller database programming console process and thread Network security assembly language Hardware programming software Design Engineering Standard Rules. The generation of programming languages or tools including, but not limited to, the following ranges:
C/c++/c# Write
Java Write generation
It generation
Python writes
Tutoring Programming Jobs
The MATLAB Generation writes
Haskell writes
Processing Write
Linux Environment Setup
Rust Generation Write
Data Structure assginment Data structure generation
MIPS Generation Writing
Machine Learning Job Writing
Oracle/sql/postgresql/pig database Generation/Generation/Coaching
Web development, Web development, Web site jobs
Asp. NET Web site development
Finance insurace Statistics Statistics, regression, iteration
Prolog write
Computer Computational Method Generation
Because of professional, so trustworthy. If necessary, please add qq:99515681 or e-mail:[email protected]
: Codinghelp
Java Eclipse job generation, Generation Eclipse experiment