CS 102 - Computers Programming II Review of CS 101 Dr. H. Assadipour 1. What is the output of this program? public class Q_01 int x=3; int y=5; double z=9; System.out.println("a: " + y/x); System.out.println("b: " + z/x); System.out.println("c: " + y%x); x=y; y=x; System.out.println("d: " + x); System.out.println("e: " + y); 2. What is the output of this program? public class Q_02 for(int i=0; i<3; i++) System.out.println(i); for(int j=i; j<3; j++) System.out.print(j + "\t"); System.out.println(); a: 1 b: 3.0 c: 2 d: 5 e: 5 0 0 1 2 1 1 2 2 2 3. What is the output of this program? public class Q_03 public static void main( String args[] ) int y, x = 0, total = 0; while ( x < 10 ) y = (int)math.pow(x,2); System.out.println("y = " + y ); total += y; x+=2; System.out.println( "Total is: " + total ); y = 0 y = 4 y = 16 y = 36 y = 64 Total is: 120 1
4. What is the output of this program? public class Q_04 int n=1; while(n<=128) n=2*n; System.out.println(n); 2 4 8 16 32 64 128 5. What is the output of this program? public class Q_05 int[] a=1,2,6,3, 5; for(int i=0; i<a.length; i++) System.out.print(a[i]+ "\t"); System.out.println(); for(int i=0; i<a.length; i++) a[i]=a[i]+i; for(int i=0; i<a.length; i++) System.out.print(a[i] + "\t"); 256 1 2 6 3 5 1 3 8 6 9 6. Consider the array declaration double [] x = new double[5]; (a) How many elements does x have? (b) What is the type of x[3]? (c) What is the smallest index i such that x[i] is valid? (d) What is the largest index i such that x[i] is valid? (e) What is the value of x.length? (a) 5 (b) double (c) 0 (d) 4 (e) 5 2
7. Suppose you are designing a class. (a) What would you use to represent the state of an object? (b) What are accessor methods used for? (c) What are constructors used for? (d) Why is it useful to implement tostring()? (e) Why is it useful to make instance variables private? 8. What is the output of this program? public class Q_08 int x=0; for(int i=1; i<=6; i++) x=x+i; System.out.println("i = " + i + "\t" + x); System.out.println("The final answer: " + x); 9. What is the output of this program? public class Q_09 int x = 3; switch(x) case 3: x += 3; case 4: break; case 5: x += 2; System.out.println("x = " + x); case 6: x ++; case 7: x +=2; case 8: x --; case 9: x +=3; System.out.println("x = " + x); (a) data (b) getters/readers (c) instantiation (d) nice display of object s contents (e) encapsulation i = 1 1 i = 2 3 i = 3 6 i = 4 10 i = 5 15 i = 6 21 The final answer: 21 X = 6 3
10. What is the output of the following program? import java.util.scanner; public class Q_10 public static void main (String[] args) int ans = 35, x = 50, y = 50; if(x > y) ans = x + 10; x -= y; else ans = y + 10; y += x; System.out.println("x = " + x); System.out.println("y = " + y); System.out.println("ans = " + ans); 11. What is the output of the following program? public class Q_11 String s1 = "abc"; String s2 = "def"; String s3 = s1; s2 = "xyz"; System.out.println(s1 + \n + s2 + \n + s3); 12. What is the output of the following program? public class Q_12 int x= 0; int y= 0; for (int z = 0; z <= 5; z++) if (( ++x > 1 ) && (++y > 1)) x++; System.out.println("x = " + x + "\t" + "y = " + y); 13. What is the output of the following program? public class Q_07 int i1 = 5; String s1 = "7"; int i2 = 6; System.out.println(i1 + i2 + s1); Your Answer: x = 50 y = 100 ans = 60 Your Answer: abc xyz abc Your Answer: x = 10 y = 5 Your Answer: 117 4
14. Write a program to display the dimensions of a letter-size (8.5 x 11 inches) sheet of paper in millimeters. There are 25.4 millimeters per inch. Use constants and comments in your program. import java.util.scanner; public class Q_14 float w, h; Scanner input = new Scanner(System.in); System.out.print("Please enter w and h: "); w = input.nextfloat(); h = input.nextfloat(); w *=25.4; h *=25.4; System.out.println("Dimensions in mm are: " + w + "\t" + h); Please enter w and h : 11 8.5 Dimensions in mm are: 279.4 215.9 15. Write a program to read integers until a zero is input. The program then prints the sum of the integers that were input and the number of negative integers that were input. import java.util.scanner; public class Q_15 int x, sum = 0, Neg_Counter = 0; Scanner input = new Scanner(System.in); System.out.print("Enter an ineger: "); x = input.nextint(); while(x!=0) System.out.print("Enter an ineger: "); x = input.nextint(); sum += x; if (x<0) Neg_Counter++; Enter an ineger: 8 Enter an ineger: 7 Enter an ineger: 8 Enter an ineger: -5 Enter an ineger: -6 Enter an ineger: 0 Sum = 4 # of negative #'s = 2 System.out.println(" Sum = " + sum); System.out.println(" # of negative #'s = " + Neg_Counter); 5
16. Write a program that reads an array of integers, e.g, [23, 34, 24, 33, 12] and displays the average and the standard deviation of the elements of the array. You must include two static methods one named mean (for the mean) and one named std_dev (for standard deviation). mean n x n i ( ) 2 i 1 x i 1 i mean n std _ dev Show a sample output with the expected results. n 1 import java.text.decimalformat; public class Q_16 int[] a = 23, 34, 24, 33, 12; DecimalFormat fmt = new DecimalFormat("0.000"); System.out.println("Mean = " + fmt.format (mean(a, a.length))); System.out.println("Std Dev = " + fmt.format(standarddeviation(a, a.length))); public static double mean (int[] numbers, int count) double sum = 0.0; for (int i=0; i < count; i++) sum += numbers[i]; return sum / count; public static double standarddeviation (int[] numbers, int count) double result = 0.0; double mean = mean(numbers, count); for (int i=0; i < count; i++) result += Math.pow (numbers[i] - mean, 2); return Math.sqrt (result / count-1); Mean = 25.200 Std Dev = 7.922 6
17. Given the following program, answer the following questions: import java.text.decimalformat; class SphereTest // Creates and exercises some Sphere objects. public static void main (String[] args) DecimalFormat fmt = new DecimalFormat("0.00"); Sphere s1 = new Sphere (5); System.out.println (s1); s1.setdiameter(9); System.out.println (s1); System.out.println("The area = " + fmt.format(s1.area(s1.getdiameter()))); System.out.println("The volume = " + fmt.format(s1.volume(s1.getdiameter()))); public static class Sphere private int diameter; // Sets up this Sphere object with the specified data. public Sphere (int diam) diameter = diam; // Accessors/Getters/Read & Mutators/Setters/Write public int getdiameter () return diameter; public void setdiameter (int diam) diameter = diam; public double area (int diameter) return Math.PI*Math.pow(diameter, 2); The area = 254.47 public double volume (int diameter) return (Math.PI/6.)*Math.pow(diameter, 3); public String tostring () DecimalFormat fmt = new DecimalFormat("0.00"); return "Diameter: " + diameter + "\t Area = " + fmt.format(area(diameter))+ "\t Volume = " + fmt.format(volume(diameter)) ; Sample run: a. Name of the Driver program: b. Name of the class: c. Constructor s name: d. An Accessor method s name: e. A Mutator method s name: f. An object s name created in the driver: g. An object passed onto print/println invokes: Diameter: 5 Area = 78.54 Volume = 65.45 Diameter: 9 Area = 254.47 Volume = 381.70 The volume = 381.70 7
18. The following program uses an array as input to a method (FindMax) and returns the maximum value. public class Seven int [] values = 23, 78, 14, 56, 90, 101, 32, 34; System.out.println("Max = " + FindMax (values)); public static int FindMax(int[] x) int largest = x[0]; for(inti = 1; i<x.length; i++) if(x[i] > largest) largest = x[i]; return largest; Max = 101 19. The following program searches for a value in an array, if found replaces it with the last element of the array. Import java.util.scanner; public class Five intpos = 0; Please enter a value: 56 int [] values = 23, 78, 14, 56, 90,101,32, 34; Scanner input = new Scanner(System.in); Found at position: 3 System.out.print("Please enter a value: "); intsearchvalue = input.nextint(); 23 78 14 34 90 101 32 34 boolean found = false; while(pos<values.length&&!found) if(values[pos] == searchvalue) found = true; else pos++; if(found) System.out.println("Found at position: " + pos); else System.out.println("Was not found"); If(found == true) values[pos] = values[values.length-1]; for(inti = 0; i<values.length; i++) System.out.print(values[i] + " "); 8