public class Test { public static void main( String[] args ) { Count myCount = new Count(); int times = 0; for (int i = 0; i < 100; i++) increment(myCount, times); System.out.println("count is " + myCount.cnt); System.out.println("times is " + times); } public static void increment(Count c, int tm) { c.cnt++; tm++; } } public class Count { public int cnt; public Count(int num) { cnt = num; } public Count() { cnt = 0; } } output:
count is 100 times is 0
String s1 = "CSC 211"; String s2 = s1; String s3 = "CSC 211";Assume that s1 and s3 have different memory location. What are the results of the following expressions?
s1 == s2; // (a) s2 == s3; // (b) s1.equals(s2); // (c) s2.equals(s3); // (d) s1.compareTo(s2); // (e) s2.compareTo(s3); // (f)output:
public class Car2 { /* data members */ ... private int serial_no; private static int object_counter; /* methods */ public Car2(String mdl, String clr, double fl) { ... object_counter++; serial_no = object_counter; } public static int getSerialNo() { return serial_no; } public static int getObjectCount() { return object_count; } ... } Answer:
non-static variable serial_no cannot be referenced from a static context return serial_no; ^ cannot resolve symbol variable object_count return object_count; ^ 2 errors
Answer: public static int FirstKeyIndex(int a[], int key) { for(int i=0; i<a.length; i++) if(a[i]==key) return i; return -1; } public static int LastKeyIndex(int a[], int key) { int value =-1; for(int i=0; i<a.length; i++) // you may (int i=a.length-1; i>=0; i--) in the above version if(a[i]==key) value = i; return value; }
double[] a = new double[ 50 ];write a loop that fills the first cell with 0.5 and the second cell with 1.2, and each succeeding cell with the sum of its two predecessors. For example, the third cell would contain 1.7.
import ArrayFns; public class ATest { public static void main(String[] args) { int[] myArray = { 6, 20, 41, -3, 47, 0, -3 }; int min; // TO-DO (1): Assign min to the minimum value. // TO-DO (2): Print min to the terminal. // TO-DO (3): Reverses A. // TO-DO (4): Print all elements in A to the terminal. } }
public interface Property { public String get(); public void set(String s) {} } public class C implements Property { private String s; public C() { s = ""; } public String get() { return s; } }