A. The first question
1. Title: Create a cartridge type, comprising a rectangle, and the high volume of the three member variables, a constructor method to initialize member variables, and calculating the volume, for the bottom two functional means, the input in the main category length, width high, the cylinder volume calculation, enter the new length, width, height, creating a new rectangle, the method using soled soled, the cylinder volume is calculated again.
2. Code and comments:
(1) juxing.java:
/ ** Create a zhuti class, class contains length, depth, and height variables, a construction method for a member variable initialization. Input in the main category length, width and height values, invoke methods tiji output class * / Import Classes in java.util *. ; Public class Juxing { // define a main class public static void main (String [] args) { // definition master method Scanner in = new new Scanner (the System.in); // use Scanner class creates objects zhuti a = new new zhuti (); of System.out.print ( "input width:"); // input data a = .kuan in.nextDouble (); of System.out.print ( "enter the length:" ); a.chang = in.nextDouble (); Of System.out.print ( "High Input:" ); a.gao = in.nextDouble (); System.out.println ( "circumference:" + a.zhouchang ()); // the output data System.out .println ( "area:" + a.mianji ()); of System.out.print ( "volume:" + a.tiji ()); } }
(2) zhuti.java:
class zhuti { // definition of a class, comprising four two member variables and methods Double Chang, Kuan, Gao, zhouchang, mianji, V; Double mianji () { // method of calculating the area of the return Chang * Kuan; } Double zhouchang () { // calculation method circumference return (Chang Kuan +) * 2 ; } Double tiji () { // volume calculation method of return (mianji () * Gao); } }
3. Run Results:
1. Title: Design class called MyInteger, comprising:
(1) int-type data field value
(2) a method configured, when a value specified int, the object created MyInteger
(3) access to the data field value and modifier
(4) isEven () and isOdd () method, if the current object is even or odd, returns true
(5) The method of class isPrime (MyInteger i), determines whether the specified value is a prime number, returns true
MyInteger objects created in the main category, each class verification MyInteger method.
2. Code and comments:
/ ** Create a MyInteger class member variables defined number, construction methods, and there are number of parameters, determines a parameter passed in the number of parity method, prime number determination method * / Package cn.edu.ccut.po; Import Java .util.Scanner; public class MyInteger { public static void main (String [] args) { Scanner iNPUT = new new Scanner (the System.in); System.out.println ( "Please enter an integer:" ); int a = iNPUT .nextInt (); MyInteger n- = new new MyInteger (A); System.out.println ( "whether it is odd:" + n.isOdd ()); System.out.println ("是否是偶数:"+n.isEven()); System.out.println("是否是素数:"+n.isPrime(n)); } static int number; public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public MyInteger(int number) { this.number=number; } boolean isEven(){ if(number%2==0) return true; else{ return false; } } boolean isOdd(){ if(number%2==1) return true; else{ return false; } } static boolean isPrime(MyInteger i){ int x=0; for(int j=2;j<number;j++){ if(number%j==0){ x++; } } if(x==0) return true; else return false; } }
3. Run Results: