Chapter 6 Introduction Categories (2)

xiaoxiao2021-03-06  45

Boxdemo4.java

// Now, Volume () returns the volume of a box.package Chapter6; class Box4 {double width; double height; double depth; // compute and return volume double volume () {return width * height * depth;}} public class BoxDemo4 {public static void main (String [] args) {Box4 mybox1 = new Box4 (); Box4 mybox2 = new Box4 (); double vol; // assign values ​​to mybox1's instance variables mybox1.width = 10; mybox1.height = 20; mybox1.depth = 15; // assign different values ​​to mybox2's instance variables mybox2.width = 3; mybox2.height = 6; mybox2.depth = 9; // get volume of first Box vol = mybox1.volume () System.out.Println ("Volmue IS" VOL); // Get Volume of Second Box Vol = MyBox2.Volume (); System.out.Println ("Volmue IS" VOL);}}

Boxdemo5.java

// This program uses a parameterized methodpackage Chapter6; class Box5 {double width; double height; double depth; // compute and return valume double volume () {return width * height * depth;} // sets dimensions of Box void setDim ( Double W, Double H, Double D) {width = W; Height = H; DEPTH = D;}} public class boxdemo5} public class boxdemo5} public class boxdemo5 {public static void main (String [] args) {box5 mybox1 = new box5 (); box5 mybox2 = New Box5 (); Double Vol; // Initialize Each Box MyBox1.Setdim (10, 20, 15); MyBox2.Setdim (3, 6, 9); // Get Volume of First Box Vol = MyBox1.Volume (); System.out.println ("Volmue IS" VOL); // Get Volume of Second Box Vol = MyBox2.Volume (); System.out.Println ("Volmue IS" Vol);}} BoxDemo6.java

/ * * Here, Box uses a constructor to initialize the dimensions of a box * / package Chapter6;. Class Box6 {double width; double height; double depth; // This is the constructor for Box6 Box6 () {System.out. .println ( "Constructing Box"); width = 10; height = 20; depth = 15;} // computer and return valume double volume () {return width * height * depth;}} public class BoxDemo6 {public static void main (String [] args) {// Declare, Allocate, And Initialize Box6 Objects Box6 mybox1 = new box6 (); box6 mybox2 = new box6 (); double vol; // get volume of first box vol = mybox1.volume () System.out.Println ("Volmue IS" VOL); // Get Volume of Second Box Vol = MyBox2.Volume (); System.out.Println ("Volmue IS" Vol);}} BoxDemo7.java

/ * * Here, Box uses a parameterized constructor to initialize the * dimensions of a box * / package Chapter6;. Class Box7 {double width; double height; double depth; // This is the constructor for Box6 Box7 (double w,. double h, double d) {width = w; height = h; depth = d;} // computer and return valume double volume () {return width * height * depth;}} public class BoxDemo7 {public static void main (String [] args) {// Declare, Allocate, and Initialize Box6 Objects Box7 mybox1 = New Box7 (10, 20, 15); Box7 mybox2 = new box7 (3, 6, 9); Double Vol; // Get Volume Of First Box vol = mybox1.volume (); system.out.println ("Volmue IS" VOL); // Get Volume of Second Box Vol = MyBox2.Volume (); System.out.Println ("Volmue IS" Vol }} Stack.java

// this class defines an INTEGER Stack That Can Hold 10 Values.Package Chapter6; Class Stack {Int Stck [] = New Int Int [10]; Int Tos; // Initialize Top-of-Stack Stack () {TOS = -1 } // Push An Item ONTO THE STACK VOID PUSH (IT ITEM) {IF (TOS == 9) System.out.Println ("Stack if Full."); Else STCK [ TOS] = Item;} / / POP and item from the stack int pop () {if (TOS <0) {system.out.println ("stckk underflow"); return 0;} else return stck [tos--];}}

TestStack.java

转载请注明原文地址:https://www.9cbs.com/read-54732.html

New Post(0)