Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Java Introductory Lab Java Introductory Lab Part 1: Compiling and running a program 1. Create the program Prog1.java (capitalization is important) in your favorite text editor, and enter in the following program. import java.util.*; class Prog1 { /* This space intentionally left blank. Part 2 of this lab should be inserted here when you get there. */ public static void main(String args[]) { // Create an list of numbers int arraySize = 10; // Note that this is using the new generic classes in Java 1.5. // numArray is an ArrayList containing integers. ArrayList numArray = new ArrayList(arraySize); // Fill the array with random integers int i; for (i=0; i < arraySize; i++) numArray.add((int)(Math.random()*50)); // Display the integers to the screen. numArray is an ArrayList of // Integer objects, so number iterates through each element in the // ArrayList. (This is new in Java 1.5). for (Integer number : numArray) System.out.print(number + " "); System.out.println(); } } 2. Compile the program by typing "javac Prog1.java" at a command prompt. Debug if you have any compiler errors. 3. Type "ls" to look at your files. You should see Prog1.java, which is the source code. You should also see Prog1.class, which is the bytecode produced by the compiler. 4. Run your program by typing "java Prog1" (do not include the .class at the end of it). This will run your program. What does it do? Part 2: Adding class methods to the class Prog1 1. The following code adds a method (function associated with a class) to your class Prog1. Add the following code in the spot indicated by the comments in the above program. private static int addup(ArrayList numbers) { int total=0; for (Integer number : numbers) total += number; return total; } 2. Add code to the main function to call addup and display the answer to the screen. Utilize the fact that this is a static method. 3. Add another method to the class to sort the numbers in the array. Add code to main to display the sorted array to the screen.   Part 3: Creating your own instantiable classes 1. In your text editor, start a new program called Prog2.java. Insert the following code: import java.util.*; class NumberList { private ArrayList numArray; private int arraySize = 10; public NumberList() { numArray = new ArrayList(arraySize); for (int i=0; i < arraySize; i++) numArray.add((int)(Math.random()*50)); } public void displayNumbers() { for (Integer number : numArray) System.out.print(number + " "); System.out.println(); } } class Prog2 { public static void main(String args[]) { NumberList list1 = new NumberList(); NumberList list2 = new NumberList(); NumberList list3 = new NumberList(); list1.displayNumbers(); list2.displayNumbers(); list3.displayNumbers(); } } 2. Add a public method to the class NumberList to sort the numbers. Add code to main to display the sorted numbers when you run your program. 3. Add a second constructor to NumberList which allows the array size to be set. 4. Change main so that instead of three different variables (list1, list2, list3), an array of NumberList is created and used. 5. Add a static variable (class variable) to the class NumberList which keeps track of how many NumberList objects have been instantiated. Update it appropriately in the constructor. Provide a static method to print out the value of this variable.