Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Wednesday, March 03, 2010 11:25 AM http://logos.cs.uic.edu/Examples%20And%20Notes/107%20Class%20Notes/Programs%20Done%20in%20Class/ // Copy constructor public Employee( Employee otherEmployee) { name = otherEmployee.name; hourlySalary = otherEmployee.hourlySalary; startDate = otherEmployee.startDate; } To fix this, in the copy constructor for this class, always call the copy constructor of any nested classes. 107 Page 1 import java.io.*; import java.util.Scanner; class ManyVariables { public static void main(String[] args) { int score1, score2, score3; // local variables Scanner keyboard = new Scanner( System.in); // Scanner for keyboard input System.out.println( "Enter 3 scores to be averaged:"); score1 = keyboard.nextInt(); score2 = keyboard.nextInt(); score3 = keyboard.nextInt(); System.out.print( "The scores are: " ); System.out.println( score1 + ", " + score2 + ", " + score3); System.out.print( "The average is: " ); System.out.println( (float)(score1+score2+score3)/3); } } Pasted from <http://logos.cs.uic.edu/Examples%20And%20Notes/notes/Java/ArraysAverageGrades/manyVariables/ManyVariables.java > import java.io.*; import java.util.Scanner; class OneVariableLoop { public static void main(String[] args){ int score = 0; // local variables int count = 0; // count how many scores are entered int sum = 0; // used to accumulate the sum Scanner keyboard = new Scanner( System.in); // Scanner for keyboard input System.out.println( "Enter scores to be averaged, followed by a -1:"); while (score != -1) { score = keyboard.nextInt(); if (score != -1) count++; sum += score; } System.out.println( "Can't print out what the scores are, they're gone... " ); System.out.print( "The average is: " ); System.out.println( (float)sum/count); } 107 Page 2 } } Pasted from <http://logos.cs.uic.edu/Examples%20And%20Notes/notes/Java/ArraysAverageGrades/oneVariableLoop/OneVariableLoop.java > import java.io.*; import java.util.Scanner; class UsingAnArray { public static void main(String[] args) throws IOException { int[] scores; // declare the array scores = new int[3]; // initialize the array Scanner keyboard = new Scanner( System.in); // Scanner for keyboard input System.out.println( "Enter 3 scores to be averaged:"); scores[0] = keyboard.nextInt(); scores[1] = keyboard.nextInt(); scores[2] = keyboard.nextInt(); System.out.print( "The scores are: " ); System.out.println( scores[0] + ", " + scores[1] + ", " + scores[2]); System.out.print( "The average is: " ); System.out.println( (float)(scores[0]+scores[1]+scores[2])/3); } } Pasted from <http://logos.cs.uic.edu/Examples%20And% 20Notes/notes/Java/ArraysAverageGrades/UsingAnArray/UsingAnArray.java> To declare an array, we use: type[ ] arrayName; where type can be any type (e.g. char, int, float), arrayName is an identifier you make up, and size indicates how many items will be stored in the array. We then must initialize the array using: arrayName = new type[ size]; For example int[ ] scores; scores = new int[ 10] ; declares and initializes an array called theScores that can hold 10 integers. In the array shown above the first element is the 0th element, and the last is the 9th element. This element number is known as the index (a.k.a. the subscript) , which starts at 0 and goes to n-1 for an array of n elements. Pasted from <http://logos.cs.uic.edu/Examples%20And%20Notes/notes/Java/ArraysAverageGrades/Arrays.htm> class UsingAnArrayB { final int ARRAY_SIZE = 10; // declare a constant for the array size // note how easy it is to change this 107 Page 3 // note how easy it is to change this public static void main(String[] args) { UsingAnArrayB instance; instance = new UsingAnArrayB(); // use the default constructor instance.doit(); // now call the method } public void doit() { int[] scores; // declare the array scores = new int[ ARRAY_SIZE]; // initialize the array //…. the rest of the code ... Pasted from <http://logos.cs.uic.edu/Examples%20And%20Notes/notes/Java/ArraysAverageGrades/UsingAnArrayB/UsingAnArrayB.java> public static void main(String[] args) { // …. Other code ... getScores( scores); // call the method to get the scores // changes ARE reflected back! // ….. Other code ... } public void getScores(int[] values) { // Prompt for scores and enter them. Use the built-in array length. System.out.println( "Enter " + ARRAY_SIZE + " scores to be averaged:"); for (int i=0; i<values.length; i++) { values[i] = keyboard.nextInt(); } } Pasted from <http://logos.cs.uic.edu/Examples%20And% 20Notes/notes/Java/ArraysAverageGrades/UsingAnArrayWithFunction/UsingAnArrayWithFunction.java> class UsingAMethodToSwap { public static void main(String[] args) { // Instead of the two lines below, we could have: // int[] values = {3,7}; // declare & initialize all at once int[] values; // declare the array values = new int[2]; // initialize the array values[0] = 3; values[1] = 7; System.out.println("Array contains: " + values[0] + " and " + values[1]); swapAttempt( values[0], values[1]); 107 Page 4 swapAttempt( values[0], values[1]); System.out.println("After swapAttempt(), array contains: " + values[0] + " and " + values[1]); actuallySwap( values); System.out.println("After actuallySwap(), array contains: " + values[0] + " and " + values[1]); } private static void swapAttempt( int x, int y) { int temp = x; x = y; y = temp; } private static void actuallySwap( int[] values) { int temp = values[0]; values[0] = values[1]; values[1] = temp; } } Pasted from <http://logos.cs.uic.edu/Examples%20And%20Notes/notes/Java/ArraysAverageGrades/UsingAMethodToSwap/UsingAMethodToSwap.java > 107 Page 5