Ans: An instance variable can be used anywhere in the class. A local variable can only be used in the method where is is defined. An instance variable represents the state (current condition) of the object. An instance variable can be declared with one of the accessibility modifers private, protected, public. A local variable is not declared with a modifier.
int[ ] a; for(i = 0; i <= a.length - 1; i++) a[i] = i;Ans: The array a is never instantiated. The variable i is not declared.
Ans:
public static void main(String[ ] args) { for(int i = 0; i <= args[0].length; i++) System.out.println(args[0].charAt(i)); }
public static void main(String[ ] args) { String[ ] a = { "watermelon", "date", "lemon", "orange", "grapefruit" }; String temp; int i; for(i = 1; i <= 4; i++) { temp = a[i]; a[i] = a[i - 1]; a[i - 1] = temp; } for(i = 0; i <= 4; i++) System.out.print(a[i].charAt(i)); } Ans: i a[0] a[1] a[2] a[3] a[4] temp ---+----------+----------+----------+----------+----------+---------- 1 watermelon date lemon orange grapefruit date 2 date watermelon lemon orange grapefruit lemon 3 date lemon watermelon orange grapefruit orange 4 date lemon orange watermelon grapefruit grapefruit date lemon orange grapefruit watermelon Output: deapr
public class A { int a; String b; public A( ) { this(7, "goose"); } public A(int intVal, String strVal) { a = intVal + 5; b = "s" + strVal; } public void update( ) { a += 5; b += "u"; } public void update(int intVal) { a += intVal; b += String.valueOf(intVal); } public String toString( ) { return a + " " + b; } } public class B extends A { public void update( ) { a += 7; b += "v"; } } public class Main { public static void main(String[ ] args) { int i; A[ ] x = new A[3]; x[0] = new A(5, "fox"); x[1] = new B( ); x[2] = x[0]; x[0].update( ); x[1].update( ); x[2].update(2); for(i = 0; i <= 2; i++) System.out.println(x[i]); } } Ans: 0 1 2 +---+ +---+---+---+ | o----->| o | o | o | +---+ +-|-+-|-+-|-+ x | +-----------+ x | | | ++ +---+ | | | | V V V a b a b -----+----- ------+------ 10 sfox 7 goose 15 sfoxu 14 goosev 17 sfoxu2 Output: sfoxu goosev sfoxu
Ans:
public class GroceryItem { private String name; private int barCode; private double price; public GroceryItem(String theName, int theBarCode, double thePrice) { name = theName; barCode = theBarCode; price = thePrice; } public double getPrice( ) { return price; } public void setPrice(double thePrice) { price = thePrice; } public String toString( ) { return String.format("%s %d %.2f", name, barCode, price); } }
2% Milk 254365786 2.65Then test the toString, getPrice, and setPrice methods.
public static void main(String[ ] args) throws FileNotFoundException { Scanner s = new Scanner(new FileReader("c:\\GroceryInfo.txt")); GroceryItem g = new GroceryItem(s.next( ), s.nextInt( ), s.nextDouble( )); System.out.println(g); g.setPrice(2.85); System.out.println(g.getPrice( )); }
public static void main(String[ ] args) { int[ ] a = new int[100]; for(int i = 0; i <= 99; i++) a[i] = Math.random( ); int max = 0; for(int i = 0; i <= 99; i++) if (a[i] > max) max = a[i]; System.out.println("Max value is " + max); }