Topic one:
5 based on the operation, creating 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 change method bottom two functions. Enter the length in the main category, width, height, calculated cylinder volume. Enter the new length and breadth, create a new rectangle, the method using soled soled, again calculated column volume
1, code implementation:
(1) Class --mian main categories: two objects comprising
/** * Main class mian class contains only one primary method --main Method * The method creates two main objects, namely, objects and Zhu shuai method sun class rectangle class * The method of using the sun * length and width of the input values are assigned to two longs rectangle member variable classes and width * Shuai method using the input value is assigned to the high cylindrical member variable classes tall Zhu * Call bulkV method to obtain the volume of the cylinder * * Length of the input value and the new value assigned to the new width change method by Zhu shuai class object used to modify the values of length and width values * High values inputted by the method shuai member variable is assigned a tall Zhu class modifier setTall (), thereby changing the value of a member variable tall * Call bulkV new method to obtain the volume of a cylinder */ package lei; import java.util.*; public class mian { public static void main(String[] args) { @SuppressWarnings("resource") Scanner reader=new Scanner(System.in); sun rectangle = new new rectangle (); // Create an object rectangle class sun sun.longs reader.nextDouble = (); // input end of the length, using the sun object is assigned the class rectangle longs value sun.width = reader. nextDouble (); // low input is wide, the width value using the sun rectangle object is assigned the class Double Tall = reader.nextDouble (); // input a cylindrical high Zhu shuai = new new Zhu (Tall, Sun); // Create an object Zhu shuai class shuai.bulkV (Sun); // call the method to calculate the volume method bulkV Double A = reader.nextDouble (); // enter new length value Double B = reader.nextDouble (); // enter a new width value shuai.change (A, B); // modify the width value and length value Double H = reader.nextDouble (); // input a new value into the high shuai.setTall (H); // Modify a high value shuai.bulkV (Sun); // call the method to calculate the volume method bulkV } }
(2) Rectangular Class --rectangle categories: variable member comprises a double four, two member variables and access modifier, a method parameter return value of a double
/** * Create a rectangle class rectangle class * Four double-type member variables * Width were created accessor and modifiers for the member variable longs, * Create the area of a rectangle with a calculated parameters, the method returns a value of type double --areas Method */ Package Penalty for LEI; // create moments row class public class of the Rectangle { Double longs, width, Area, Perimeter; // create and longs access modifier public Double getLongs () { return longs; } public void setLongs(double longs) { this.longs=longs; } // Create a width of and access modifier public Double getWidth () { return width; } public void setWidth(double width) { this.width=width; } Double Areas ( Double Longs, Double width) { // create a method to calculate the area Area Longs = * width; return Area; } }
(3) Zhu categories: variable comprising three members, a member variable accessors and modifiers, a construction method, two methods
/** * Create a class zhu * Two double-type member variables tall, bulk rectangle and a member variable of type sun * Create a member variable tall and access modifier * Create a constructor method of long-term variable initialization * Create a bulk method returns no value calculated cylinder volume * Create a free way to achieve change return value to change the end of the function * / Package Penalty for LEI; public class Zhu { Double Tall; // high Double Bulk; // volume Rectangle Sun; // rectangle // Tall and access modifier public Double getTall () { return Tall; } public void setTall(double tall) { this.tall=tall; } // Create a constructor method of initialization of member variables (Zhu Double Tall, Rectangle Bian) { the this .tall = ; Tall the this .sun = Bian; } // Create a bulkV method, calculation of the cylinder volume void bulkV (Rectangle Sun) { bulk=tall*sun.areas(sun.longs ,sun.width); System.out.println ( "volume of the cylinder is:" + Bulk); } // Create a change implemented method soled function void change ( Double width, Double Longs) { the this .sun.width = width; the this .sun.longs = Longs; } }
2, the test run:
Topic two:
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.
1, code implementation:
MyInteger class: contains a member variable, and the field access modifier, three methods
/** * Define a class MyInteger * Value defined as a member variable, and to access configuration value modifier * By determining whether the method is even isEven * By determining whether the method is an odd number isOdd * By determining whether the method is a prime number isPrime */ package Seven_two; import java.util.*; public class MyInteger { int value; // accessor and modifiers public int the getValue () { return value; } public void setValue(int value) { this.value=value; } static Boolean isEven ( int value) { // isEven method to determine whether an even IF (value% 2 == 0 ) { return to true ; }else { return false; } } static Boolean isOdd ( int value) { // isOdd method of determining whether an odd IF (value = 0% 2! ) { return to true ; }else { return false; } } static Boolean the isPrime ( int value) { // the isPrime determines whether a prime number for ( int I = 2; I <value; I ++ ) { IF (% I value == 0 ) { return to false ; } if(i+1==value) { return true; } } return false; } }
Object contains a main class, a main method, a class MyInteger: Text-based
/** * Enter a number, this number is assigned to int value * Create a method --sun method MyIteger class The method of using the sun * value, respectively isEven method, isOdd method, isOrime method assigned */ package Seven_two; import java.util.*; public class Text { public static void main (String [] args) { // the TODO Auto-Generated Stub Method System.out.println ( "Enter a number" ); @SuppressWarnings("resource") Scanner deara = new Scanner(System.in); int value = deara.nextInt(); Sun MyInteger = new new MyInteger (); // Create a class method --sun MyInteger method System.out.println ( "is an even number" + sun.isEven (value)); System.out.println ( "if an odd number" + sun.isOdd (value)); System.out.println ( "whether the prime number" + sun.isPrime (value)); } }
2, the test run