Master the basic Java language and object-oriented comprehensive programming technology and methods, and have a more comprehensive and in-depth understanding of the technical connotation of object-oriented programming.
1, Reading procedure question 1
Please read the following program to determine its output. Then run the program on the computer to verify whether the output result of your analysis is correct.
package Practice3; class Yuan { int r; String name; static int z= 5; Yuan(int r){ this.r=r; } int a(){ return z*r*r; } int p(){ return 5*z*r; } void value (){ name="china"; } } class testOverride extends Yuan { int h; String name; testOverride(int r,int h1) { super(r); h=h1; } int a(){ value(); return 2*super.a()+p()*h; } void value() { super.value(); name="America"; System.out.println("\""+this.name+"\""); System.out.println(super.name); } public static void main(String args[]) { Yuan y= new Yuan(4); Yuan yz= new testOverride(5,2); System.out.println(y.a()); System.out.println(yz.p()); System.out.println(yz.a()); } }
Just run it yourself. The results are as follows:
80 125 "America" china 500
2, Reading procedure question 2
Please read the following program to determine its output. Then run the program on the computer to verify whether the output result of your analysis is correct.
public class TestTransOfValue { public static void main(String args[]) { double val; StringBuffer sb1, sb2; String sb3; char s[]={'a','p','p','l','e'}; val = 5.8; sb1 = new StringBuffer("apples"); sb2=new StringBuffer("pears"); sb3 = new String("pear"); modify(val, sb1, sb2,sb3,s); System.out.println(val); System.out.println(sb1); System.out.println(sb2); System.out.println(sb3); System.out.println(s); } public static void modify(double a, StringBuffer r1, StringBuffer r2,String r3,char s[] ) { a = 6.8; r1.append(" taste good"); r2=null; r3="banana"; s[2]='R'; } }
The results are as follows:
5.8 apples taste good pears pear apRle
3, Reading procedure question 3
Please read the following program carefully and analyze the structure and output results of the program. Then run the program on the computer to verify the output results. (pay attention to the key understanding: internal class, object internal class and static internal class)
package Practice3; public class Outer { public Outer() { System.out.println("OuterClass Object!"); } private class Inner1 { private Inner1(String s){ System.out.println(s);} } static class Inner2 { Inner2(String s){ System.out.println(s);} } public static void main(String[] args) { Outer ob= new Outer(); Outer.Inner1 ib1 = ob.new Inner1("InnerClass1 Object!"); //Inner1 ib1 = ob.new Inner1("InnerClass1 Object!"); Inner2 ib2 = new Inner2("InnerClass2 Object!"); } }
result:
OuterClass Object! InnerClass1 Object! InnerClass2 Object!
Please try again to:
Outer.Inner1 ib1 = ob.new Inner1("InnerClass1 Object!");
Replace with:
ob.Inner1 ib1 = ob.new Inner1("InnerClass1 Object!");
See what happens.
The result is:
OuterClass Object! InnerClass1 Object! InnerClass2 Object!
4, Reading procedure question 4
Please read the following program carefully and analyze the structure and output results of the program. Thus, we can understand interfaces, abstract classes, inheritance, implementation interfaces, and further understand polymorphism.
package Practice3; interface Food { void doEat();} // doEat() is the way to eat food abstract class Fruit{ } //Fruit abstract class abstract class Meat{ } //Meat abstract class class Apple extends Fruit implements Food //Apples { public void doEat() { System.out.println("I am an apple, belonging to the fruit category. You don't have to cook, I can eat it!"); } } class Beef extends Meat implements Food //Beef { public void doEat() {System.out.println("I am beef, belongs to meat, must be cooked before eating!"); } } public class Use { public static void main(String[] args) { Food f=new Apple(); f.doEat(); f=new Beef(); f.doEat(); // Two "f.doEat()" embody polymorphism } }
Output results:
I am an apple, belonging to the fruit category. You don't have to cook, I can eat it! I am beef, belongs to meat, must be cooked before eating!
Try changing the main method to:
public static void main(String args[]) { Food f=new Apple(); f.doEat(); }
How does the observation system react?
Output results:
I am an apple, belonging to the fruit category. You don't have to cook, I can eat it!
analysis:
Interface can be used as a reference type. The instance of any class implementing the interface can be stored in the variables of the interface type, and the methods in the interface implemented by the class can be accessed through these variables. The Java runtime system dynamically determines which method in the class to use. In the main method, Food f=new Apple(); This concept is used.
Then add methods to the Fruit class
abstract void doEat();
Look at the result. What conclusion can you draw?
Output results:
I am an apple, belonging to the fruit category. You don't have to cook, I can eat it!
Conclusion:
The doEat() method is added to the Fruit class, and the doEat() method is also added to the Food interface. The Apple class inherits the Fruit class and implements the Food interface.
In the Apple category, public void doeat() {system. Out. Println ("I'm an Apple. I belong to the Fruit category. You don't have to cook to eat!");} Is this sentence for the Food interface or the Fruit abstract class? The answer should be the former.
Because the interface is more abstract than the abstract class, the implementation of the interface takes precedence over the abstract class.
5, Realize the multiplication of two matrices
Write a program in which a Matrix class Matrix is designed, and finally calculate:
The Matrix class is required to meet the following requirements:
- The properties of the Matrix are:
m. N: int type, the number of rows and columns of the matrix.
ma: int type two-dimensional array to place the data of the matrix.
- Matrix methods include:
Matrix (int m, int n): construction method to set the number of rows and columns of the matrix.
cheng(Matrix a): multiplies the current Matrix and the formal parameter Matrix, and finally returns the result of the multiplication (Matrix object).
void print(): output matrix.
code
import java.util.Scanner; public class CalculateMatrix { static class Matrix { int m; int n; int [][]ma; public Matrix(int m, int n) { this.m = m; this.n = n; ma = new int[m][n]; } Matrix cheng(Matrix a) { int x = this.m; int y = this.n; int z = a.n; Matrix tmp = new Matrix(x,z); for (int i = 0; i < x; i++) { for (int j = 0; j < z; j++) { for (int k = 0; k < y; k++) { tmp.ma[i][j]+= this.ma[i][k] * a.ma[k][j]; } } } return tmp; } void print() { for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { System.out.print(ma[i][j]); System.out.print('\t'); } System.out.print('\n'); } } } public static void main(String[] args) { int x; int y; int z; Scanner scanner = new Scanner(System.in); System.out.println("Please enter the number of rows and columns of the first matrix"); x = scanner.nextInt(); y = scanner.nextInt(); Matrix mtx1 = new Matrix(x,y); System.out.println("Please enter each element of the matrix line by line:"); for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { mtx1.ma[i][j] = scanner.nextInt(); } } System.out.println("Please enter the number of rows and columns of the second matrix:"); while (scanner.nextInt()!=y) { z = scanner.nextInt(); System.out.println("Cannot multiply. Please re-enter the number of rows and columns of the second matrix:"); } z = scanner.nextInt(); Matrix mtx2 = new Matrix(y,z); System.out.println("Please enter each element of the matrix line by line:"); for (int i = 0; i < y; i++) { for (int j = 0; j < z; j++) { mtx2.ma[i][j] = scanner.nextInt(); } } scanner.close(); System.out.println("The matrix obtained by multiplication is as follows:"); mtx1.cheng(mtx2).print(); } }
Running screenshot
6, Use the Shape interface to output the drawing area
Design a program, which contains an interface Shape (Shape), in which there is a method to find the area of the Shape area(). Then define three classes to implement the interface: Triangle class, rectangle class and circle class. Create a one-dimensional array of Shape type in the main method, which has three elements, place three objects to represent triangles, rectangles and circles respectively, and then output the area of the three figures by cycling.
(Note: triangle area s=Math.sqrt(p*(p-a)*(p-b)*(p-c)), a, B and C are three sides, p=(a+b+c)/2)
code implementation
public class Graph { interface Shape{void area();} static class Triangle implements Shape { double a; double b; double c; public Triangle(double a,double b,double c) { this.a = a; this.b = b; this.c = c; } @Override public void area() { double p = (a+b+c)/2.0; double s = Math.sqrt(p*(p-a)*(p-b)*(p-c)); System.out.println("The area of a triangle is:"); System.out.println(s); } } static class Rectangle implements Shape { double x; double y; public Rectangle(double x,double y) { this.x = x; this.y = y; } @Override public void area() { double s = x * y; System.out.println("The area of the rectangle is:"); System.out.println(s); } } static class Circle implements Shape { double r; public Circle(double r) { this.r = r; } @Override public void area() { double s = Math.PI*r*r; System.out.println("The area of a circle is:"); System.out.println(s); } } public static void main(String[] args) { Shape[] graph = new Shape[3]; graph[0] = new Triangle(3,4,5); graph[1] = new Rectangle(6,6); graph[2] = new Circle(2); for (int i = 0; i < 3; i++) { graph[i].area(); } } }
Running screenshot
7, Write a program with three packages
(refer to the class in question 1 of assignment 2)
The first package: personnel package, which includes personnel, students and teachers.
The second package: management package. There are two classes: class and teacher. They have the attributes of student list (array composed of student objects) and teacher list (array composed of teacher objects) respectively. They have the methods to create and output classes and teachers respectively.
The third package: using the package, there is only one class (main class). In the main method, we create a teacher class object, call it method to establish a teacher with 3 teachers, and then call the method to export the teachers.
package People
//Person class package People; public class Person { private String number; private String name; private String gender; public Person(String number,String name) { this.number = number; this.name = name; } public void setNumber(String number) { this.number = number; } public void setName(String name) { this.name = name; } public String getNumber() { return number; } public String getName() { return name; } }
//Student class package People; public class Student extends Person { private String grade; public Student(String number, String name) { super(number, name); } public void setGrade(String grade) { this.grade = grade; } public String getGrade() { return grade; } }
//Teacher class package People; public class Teacher extends Person { private String department; public Teacher(String number, String name) { super(number, name); } public void setDepartment(String department) { this.department = department; } public String getDepartment() { return department; } }
Management pack package Manage
//Klass class package Manage; import People.Student; public class Klass { private String klassName; private Student[] stuList = new Student[100]; public Klass(String klassName) { this.klassName = klassName; } public void setStuList(int i, Student student) { stuList[i] = student; } public void getStuList(int n) { for (int i = 0; i < n; i++) { System.out.println(stuList[i].getName()); } } }
//Faculty class package Manage; import People.Teacher; public class Faculty { private String facultyName; private Teacher[] teaList = new Teacher[100]; public Faculty(String facultyName) { this.facultyName = facultyName; } public void setTeaList(int i, Teacher teacher) { teaList[i] = teacher; } public void getTeaList(int n) { for (int i = 0; i < n; i++) { System.out.println(teaList[i].getName()); } } }
Use package Use
//There is only one main class package Use; import Manage.Faculty; import People.Teacher; import java.util.Scanner; public class Main { public static void main(String[] args) { System.out.println("Please enter the teacher name:"); Scanner scanner = new Scanner(System.in); if (scanner.hasNext()) { String facultyname = scanner.next(); Faculty faculty1 = new Faculty(facultyname); System.out.println("Please enter the number of teachers:"); int sum = Integer.parseInt(scanner.next()); System.out.println("Please enter the number and name of each teacher:"); for (int i = 0; i < sum; i++) { String number = scanner.next(); String name = scanner.next(); Teacher teacher = new Teacher(number, name); faculty1.setTeaList(i, teacher); } System.out.println("The list of teachers of the teacher is as follows:"); faculty1.getTeaList(sum); } } }
Running screenshot