Access to objects using

Topic 1 :

5 on the basis of the job, and then creating a column type, comprising a rectangle, and the high volume of the three member variables, a constructor method to initialize member variables, and calculating the volume change method bottom two functions, in the main category input length, width, height, calculated cylinder volume, enter a new length, width, height, creating a new rectangle, the method using soled soled, the cylinder volume is calculated again.

import java.util.Scanner;
class juxing {//定义矩形类
aaa www;//成员变量
double high;
double mianji;
public juxing(aaa www,double high) {//构造方法
this.www=www;
this.high=high;
}
public void change_Www(aaa www) {//换底
this.www=www;
}
public double get_volume() {//求体积
mianji=high*www.getArea();
return mianji;
}

}
public class aaa {
double length;
double weight;
double area;
double perimeter;
public aaa(double length,double weight) {
this.length=length;
this.weight=weight;
}
public double getArea() {//面积
weight = length * Area;
return Area;
}
public Double the getPerimeter () {// perimeter
Perimeter = 2 * (length + weight);
return Perimeter;
}
public static void main (String [] args) {
Scanner Scanner new new INPUT = (the System.in);
Double length = 0.0, weight = 0.0, 0.0 = high;
System.out.println ( "enter the length, width, height:");
length = input.nextDouble ();
weight = input.nextDouble ( );
High = input.nextDouble (); // get the value of the input keyboard LWH
aaa www = new aaa (length, weight); // declare an object
Juxing Qua = new new Juxing (WWW, High);
System.out.println ( "area =" + www.getArea ());
System.out.println ( "perimeter =" + www.getPerimeter ());
System.out.println ( "volume =" + qua.get_volume ());
System.out.println ( "Soled, input length, width: ");
length = input.nextDouble (); // input value after soled
weight = input.nextDouble ();
WWW AAA new new = (length, weight);
qua.change_Www (WWW);
System.out.println ( "Area = "+ www.getArea ());
System.out.println (" perimeter = "+ www.getPerimeter ());
System.out.println (" volume = "+ qua.get_volume ());
}
}

 

Run shot

 

 

 

Question 2

MyInteger design class name, which includes: int type value a data field construction method, when the specified int value, object data created MyInteger field value and the access modifier isEven () and isOdd () method, if the current object is even or odd, class method returns true isPrime (MyInteger i), determines whether the specified value is a prime number, returns true MyInteger create objects in the main class, each class MyInteger verification method.

 

import java.util.*;
class MyInteger {
public int value;

 

public MyInteger(int value) {
this.value = value;
}

 

public int getValue () {// accessor
return value;
}

 

public void setValue(int value) {//修改器
this.value = value;
}

 

public boolean isEven () {// determines the number of parity
IF (this.value% 2 == 0)
return to true;
the else
return to false;
}

 

public boolean isOdd() {
if (this.value % 2 == 1)
return true;
else
return false;
}

 

public static boolean isPrime(MyInteger i) {//判断是否为素数
for (int j = 3; j < i.value; j++)
if (i.getValue() % j == 0)
return false;
return true;
}

 

public static void main (String [] args) {// main class
Scanner Scanner A new new = (the System.in);
System.out.println ( "Enter a number:");
int value = a.nextInt ();
= new new MyInteger myInteger MyInteger (value);
System.out.println ( "the value is" + myInteger.getValue ());
IF (myInteger.isOdd ())
System.out.println (myInteger.value + "is an odd number." );
IF (myInteger.isEven ())
System.out.println (myInteger.value + "is an even number");

System.out.println ( "enter a number");
value = a.nextInt ();
myInteger. the setValue (value);
IF (myInteger.isPrime (myInteger))
System.out.println (myInteger.value + "is a prime number");
}
}

Run shot

 

 

 

 

Guess you like

Origin www.cnblogs.com/qdkogmsdlfs/p/11567803.html