java seventh job

First, a title:

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.


 

Second, the program code :

/ * Create a zhuti class, defined length, width, higher variable, create three constructors calculate perimeter, area, volume. Value input in the main category of length and breadth, gao call tiji method for variable assignment, the output method invocation * / tiji class
import java.util *.;

public class juxing {// main class
public static void main (String [] args) {// main method
Scanner in = new Scanner (System.in) ; // reference input function, which can write input operation
zhuti haha = new zhuti (); // create an object of class M (haha object)
System.out.print ( "enter long:");
haha.chang = in.nextDouble (); // Scanner in the name of. nextInt front of the same name
System.out.print ( "input width:");
haha.kuan in.nextDouble = ();
System.out.print ( "high input:");
haha.gao in.nextDouble = ();
the System .out.println ( "area:" + haha.mianji ()); // reference member class, method name with an object reference haha +.
System.out.println ( "circumference:" + haha.zhouchang ());
of System.out.print ( "volume:" + haha.tiji ());
}
}

class zhuti {// definition of a class, which contains four two member variables and methods
double chang, kuan, gao, zhouchang , mianji, v;

double mianji () {// a method of calculating the area of the
return Kuan Chang *;
}

double zhouchang () {// calculate the circumference of a method of
return (Chang Kuan +) * 2;
}
Double tiji () {// a method of calculating the volume of
return (mianji () * Gao);
}
}


 

 

Third, the results:


 

I, entitled II:

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.


 

Second, the program code:

/ * Called MyInteger design class, define a variable of type int value, creating MyInteger data field value of the object to access and modify, create three methods are isEven (), isOdd () and the isPrime (), they effects are: determining whether the value is an even number, odd number and a prime number. Finally MyInteger be instantiated in the main category, calling the output * /
Import java.util.Scanner;
public class MyInteger {
static int value;
Private static MyInteger I;

public static int getValue () {// value accessors and modifiers
return value;
}
public static void the setValue (int value) {
MyInteger.value = value;
}
public MyInteger (int value) {
this.value = value;
}
boolean isEven () {// determines whether an even number value
iF (value% 2 == 0)
return to true;
return to false;
}
Boolean isOdd () {// determines whether an odd value
iF (value == 2%. 1)
return to true;
return to false;
}
Boolean the isPrime (MyInteger I) {// determined value is prime
int S = 0;
for (int J = 2; J <value; J ++) {
iF (value% J == 0) {

}
{the else
S =. 1;
}
IF (S == 0) {
return to false;
}
return true;
}
return false;
}

public static void main (String [] args) {// main method
Scanner Scanner in = new new (the System.in);
of System.out.print ( "Please enter an integer:");
int in.nextInt Y = ();
MyInteger g = new MyInteger (y) ; // instantiate the object G
System.out.println ( "is an even number:" + g.isEven ());
System.out.println ( "odd:" + g.isOdd ( ));
of System.out.print ( "is a prime number:" + g.isPrime (I));
}
}


 

Third, the results:

Guess you like

Origin www.cnblogs.com/wyd123/p/11562820.html