You are on page 1of 6

1) public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Input an integer: "); int n = in.

nextInt(); int d = 1; double temp = n; while (temp > .0001) { temp = temp / 2; d++; } System.out.println(n + " must be divided " + d + " times"); } 2) any value less than 3000 3) public static void main(String[] args) { int millennium = 3000; Scanner in = new Scanner(System.in); System.out.print("Please enter the current year: "); int year = in.nextInt(); int nyear = year; while (nyear < millennium) { nyear++; } System.out.println("Another " + (nyear - year) + " years to the millennium.") ; } 4) import java.util.Scanner; public class Tester { public static void main(String[] args) { for (int century = 5; century >= 1; century--) { String years = ""; if (century == 5) years = "400-499"; else if (century == 4) years = "300-399"; else if (century == 3) years = "200-299"; else if (century == 2) years = "100-199"; else if (century == 1) years = "1-99"; System.out.println ("Century " + century + " BC " + year s); }

for (int century = 1; century <= 5; century++) { String years = ""; if (century == 5) years = "400-499"; else if (century == 4) years = "300-399"; else if (century == 3) years = "200-299"; else if (century == 2) years = "100-199"; else if (century == 1) years = "1-99"; System.out.println ("Century " + century + " AD " + year s); } } } 5) import java.util.Scanner; public class Tester { public static void main(String[] args) { for (int century = -5; century <= 5; century++) { String years = ""; Math.abs(century); if (Math.abs(century) == 5) years = "400-499"; else if (Math.abs(century) == 4) years = "300-399"; else if (Math.abs(century) == 3) years = "200-299"; else if (Math.abs(century) == 2) years = "100-199"; else if (Math.abs(century) == 1) years = "1-99"; if (century < 0) System.out.println ("Century " + -century + " BC " + years); else if (century > 0) System.out.println ("Century " + century + " AD " + years); } } } 6) import java.util.Scanner; public class Tester { public static void main(String[] args) { Scanner in = new Scanner(System.in); int sum = 0; int n;

do { System.out.print("Please enter a number, 0 to quit: "); n = in.nextInt(); if (n != 0) { sum = sum + n; System.out.println("Sum = " + sum); } } while (n != 0); } } 7) No it is not an improvement because the while loop ran at least once because n is set to 1, 8) import java.util.Scanner; public class Tester { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Please enter a number, 0 to quit: "); int n = in.nextInt(); int i; for (i = 1; n * n > Math.pow(2,i); i++); System.out.println("2 raised to " + i + " is the first power of two greater than " + n + " squared"); } } 9) import java.util.Scanner; public class Tester { public static void main(String[] args) { int i = 1; while (i <= 10) { System.out.println(i + " squared equals " + i * i); i++; } } } 10) 0 1 2 3 4 11) System.out.print((i + 1) + " "); 12) 1 10 100 1000 10000

13) System.out.print (((int)Math.log10(decimals) + 1) + " "); 14) 5 4 3 2 1 15) We never went over this in class... 16) import java.util.Scanner; import java.util.Random; public class Cards { public Cards () { cards = 52; ace = 4; two = 4; three = 4; four = 4; five = 4; six = 4; seven = 4; eight = 4; nine = 4; ten = 4; jack = 4; queen = 4; king = 4; } public void draw (int drawnCards) { for (int counter = 1; counter <= drawnCards; counter++) { Random x = new Random (); if (cards > 0) { int y; do { y = x.nextInt (13) + 1; } while ((y == 1 && ace == 0) || (y == 2 && two == 0) || ( y == 3 && three == 0) || (y == 4 && four == 0) || (y == 5 && five == 0) || (y == 6 && six == 0) || (y == 7 && seven == 0) || (y == 8 && eight == 0) || (y == 9 && nine == 0) || (y == 10 && ten == 0) || (y == 11 && jack == 0) || (y == 12 && queen == 0) || (y == 13 && king == 0)); if (y == 1) { System.out.println ("Ace"); ace--; } else if (y == 2) { System.out.println (y); two--; }

else if (y == 3) { System.out.println three--; } else if (y == 4) { System.out.println four--; } else if (y == 5) { System.out.println five--; } else if (y == 6) { System.out.println six--; } else if (y == 7) { System.out.println seven--; } else if (y == 8) { System.out.println eight--; } else if (y == 9) { System.out.println nine--; } else if (y == 10) { System.out.println ten--; } else if (y == 11) { System.out.println jack--; } else if (y == 12) { System.out.println queen--; } else if (y == 13) { System.out.println king--; } cards--; } } }

(y);

(y);

(y);

(y);

(y);

(y);

(y);

(y);

("Jack");

("Queen");

("King");

private private private private private private private private private private private private private private

int int int int int int int int int int int int int int

cards; ace; two; three; four; five; six; seven; eight; nine; ten; jack; queen; king;

public static void main (String [] args) { Scanner in = new Scanner (System.in); System.out.print("Draw how many cards?: "); int cardsDrawn; do { cardsDrawn= in.nextInt (); if (cardsDrawn > 52) System.out.print ("ERROR: Not Enough Cards\nHow many cards?: "); } while (cardsDrawn > 52); Cards deck1 = new Cards (); deck1.draw (cardsDrawn); } }

You might also like