Convert the following while-loop into an equivalent for-loop as closely as possible using the code snippet bank and template below. It is not enough for the loop to be functionally identical; it needs to follow any patterns mentioned in lecture. int j = 9;while ( j > 0 ) { j–; System.out.println(j); j /= 2;} for (1. _____________; 2._____________; 3._____________) { 4. _____________; System.out.println(j);}
Category: Uncategorized
o o _Scope and Shadowing__ ) …
o o _Scope and Shadowing__ ) ( {((((((((((((((((((((((( ( o_o) /\/\/\/\/\/\/\/\/\/\/\\ `–
For the given code below, which lines are valid (will compil…
For the given code below, which lines are valid (will compile and run)? Assume each line is run independently. public class Bean { private static boolean small; private int value; public static int grow() { return 0; } public double wilt() { return 0.0; } public static void main(String[] args) { Bean obj = new Bean(); 1 Bean.wilt(); 2 Bean.grow(); 3 obj.wilt(); 4 obj.grow(); 5 System.out.println(Bean.value); 6 System.out.println(Bean.small); 7 System.out.println(obj.value); 8 System.out.println(obj.small); } } 1 : [1] 2 : [2] 3 : [3] 4 : [4] 5 : [5] 6 : [6] 7 : [7] 8 : [8]
public double addVals(double d1, double dy) { //Other code…
public double addVals(double d1, double dy) { //Other code here } Select all of the valid overloads for the method shown above.
Given the code below, what is printed? public class Operati…
Given the code below, what is printed? public class Operations { public static void main(String[] args) { int num1 = 5; int num2 = method1(num1); System.out.print(num1 + “,”); System.out.print(num2); } public static int method1(int num2) { int num3 = num2 % 4; num2 = num2 * 2; return num2; } }
What is the output of the following code? public class Fruit…
What is the output of the following code? public class Fruit { static String type = “Banana”; int cost = 2; Fruit(String type, int cost) { type = type; cost = cost; } public void print() { System.out.println(“A ” + type + ” costs ” + cost + ” dollars”); } public static void main(String[] args) { Fruit fruit1 = new Fruit(“Peach”, 5); Fruit fruit2 = new Fruit(“Strawberry”, 3); fruit1.print(); fruit2.print(); } }
You have a file Swamp.java and you have a driver class named…
You have a file Swamp.java and you have a driver class named Driver.java. Fill in the correct visibility modifiers so that the comments in the main method are upheld. public class Driver { public static void main(String[] args) { Swamp t = new Swamp(); // each of the lines below is run independently System.out.println(t.width); // compiles and runs System.out.println(t.getDepth()); // compiles and runs System.out.println(t.depth); // compile error } } —— in a separate file in a different package/directory ——— public class Swamp { 1 int width; 2 int depth; 3 int getDepth() { return depth; } /** no-argument constructor implemented **/ } 1 :[vis1] 2 :[vis2] 3 :[vis3]
Given the code below, what is printed? public class Operati…
Given the code below, what is printed? public class Operations { public static void main(String[] args) { int a = 2; int b = method1(a); System.out.print(a + “,”); System.out.print(b); } public static int method1(int b) { int c = b / 2; b = b + 4; return c; }}
Does the following code print “Durt” to the console? public…
Does the following code print “Durt” to the console? public class Test { public static void main(String[] args) { String a = “Dirt”; iToU(a); System.out.println(a); } public static void iToU(String b) { b = b.replace(‘i’, ‘u’); } }
You have a file Bank.java and you have a driver class named…
You have a file Bank.java and you have a driver class named Driver.java. Fill in the correct visibility modifiers so that the comments in the main method are upheld. public class Driver { public static void main(String[] args) { Bank t = new Bank(); // each of the lines below is run independently System.out.println(t.totalMoney); // compiles and runs System.out.println(t.getTotalMoney()); // compiles and runs System.out.println(t.id); // compile error } } —— in a separate file in a different package/directory ——— public class Bank { 1 int id; 2 double totalMoney; 3 double getTotalMoney() { return totalMoney; } /** no-argument constructor implemented **/ } 1 :[vis1] 2 :[vis2] 3 :[vis3]