Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 2a. true
- 2b. false
- 2c. true
- 2d. false
- 2e. true
- 2f. false
- 2g. false
- 2h. true
- 2i. true
- 4.
- line 5: if statement should use () parentheses, not {} brackets
- line 5: = should be ==
- line 5: smaller is out of scope here
- line 10: void should be int
- line 13: => should be >= (or better yet, no if test is needed)
- line 16: should not write variable's type of int when returning it
- line 16: int smaller is out of scope here (declare outside if or return directly)
- 6a. 13 21
- 6b. 5 6
- 6c. 6 5
- 6d. 7 11
- 8. The program's brace placement makes it so if number % 3 == 0 the number isn't odd, if I had number as 9 then it wouldn't read 9 as odd. The else statement is nested with the closest if statement so to fix it you have to do the following
- if (number % 2 == 0) {
- if (number % 3 == 0) {
- System.out.println("Divisible by 6.");
- }
- } else {
- System.out.println("Odd.");
- }
- 10.
- a = 2;
- if (x < 30) {
- x++;
- }
- System.out.println("Java is awesome! " + x);
- 12.import java.util.*;
- public class Bills {
- public static void main(String[] args) {
- Scanner pikachu = new Scanner(System.in);
- int Bills1 = Bills(pikachu, "John");
- int Bills2 = Bills(pikachu, "Jane");
- System.out.println("John needs " + Bills1 + " bills");
- System.out.println("Jane needs " + Bills2 + " bills");
- }
- public static int Bills(Scanner console, String name) {
- System.out.print("How much will " + name + " be spending? ");
- double amount = pikachu.nextDouble();
- System.out.println();
- int numBills = (int) (amount / 20.0);
- if (numBills * 20.0 < amount) {
- numBills++;
- }
- return numBills;
- }
- }
- 14.
- Scanner pikachu = new Scanner(System.in);
- System.out.print("Enter a card: ");
- String rank = pikachu.next();
- String suit = pikachu.next();
- if (rank.equals("2")) {
- rank = "Two";
- } else if (rank.equals("3")) {
- rank = "Three";
- } else if (rank.equals("4")) {
- rank = "Four";
- } else if (rank.equals("5")) {
- rank = "Five";
- } else if (rank.equals("6")) {
- rank = "Six";
- } else if (rank.equals("7")) {
- rank = "Seven";
- } else if (rank.equals("8")) {
- rank = "Eight";
- } else if (rank.equals("9")) {
- rank = "Nine";
- } else if (rank.equals("10")) {
- rank = "Ten";
- } else if (rank.equals("J")) {
- rank = "Jack";
- } else if (rank.equals("Q")) {
- rank = "Queen";
- } else if (rank.equals("K")) {
- rank = "King";
- } else { // rank.equals("A")
- rank = "Ace";
- }
- if (suit.equals("C")) {
- suit = "Clubs";
- } else if (suit.equals("D")) {
- suit = "Diamonds";
- } else if (suit.equals("H")) {
- suit = "Hearts";
- } else { // suit.equals("S")
- suit = "Spades";
- }
- System.out.println(rank + " of " + suit);
- 16.
- public static int countFactors(int n) {
- int count = 0;
- for (int i = 1; i <= n; i++) {
- if (n % i == 0) {
- count++;
- }
- }
- return count;
- }
- 18.6.800000000000001 roundoff error
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement