Answer to Question #302800 in Java | JSP | JSF for josh

Question #302800

Plan, code and compile a BowlingLane class that will model a 5 Pin Bowling Lane. The bowling lane will have 5 pins which can be either upright or knocked down. Use 5 boolean variables to represent the pins. Next, create a client program that will:

  • instantiate a bowling lane object after prompting the user for the states of each of the 5 pins (up or down);
  • Display the bowling lane object to the user in a friendly way so they can clearly see which pins are up and which pins are down.
  • Using a separate method you must build into the bowling lane object, classify the lane configuration and display it to the user. The rules are as follows:
  • If pins 2 & 3 or 3 & 4 are down, display HS - Headsplit.
  • If pins 2, 3 & 4 are all down, display A - Aces.
  • If all the pins are down, display X - Strike.
  • display the sum of the pins that have been knocked down. NOTE! The pin values are different than the pin locations. Pins 1 & 5 are worth 2 points, pins 2 & 4 are worth 3 points, and pin 3 is worth 5 points.
1
Expert's answer
2022-02-26T05:52:08-0500
public class BowlingLane {
    boolean one;
    boolean two;
    boolean three;
    boolean four;
    boolean five;

    public BowlingLane(boolean one, boolean two, boolean three, boolean four, boolean five) {
        this.one = one;
        this.two = two;
        this.three = three;
        this.four = four;
        this.five = five;
    }
}


import java.util.Scanner;

public class Main {
    public static void classify(BowlingLane bowlingLane) {
        if (!bowlingLane.one && !bowlingLane.two && !bowlingLane.three && !bowlingLane.four && !bowlingLane.five) {
            System.out.println("X - Strike");
        } else if (!bowlingLane.two && !bowlingLane.three && !bowlingLane.four) {
            System.out.println("A - Aces.");
        } else if ((!bowlingLane.two && !bowlingLane.three) || (!bowlingLane.three && !bowlingLane.four)) {
            System.out.println("HS - Headsplit.");
        }
        int points = (!bowlingLane.one ? 2 : 0) + (!bowlingLane.two ? 3 : 0) + (!bowlingLane.three ? 5 : 0)
                + (!bowlingLane.four ? 3 : 0) + (!bowlingLane.five ? 2 : 0);
        System.out.println("Points: " + points);
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter states of five bowling pins(1-Up | 2-Down):");
        BowlingLane bowlingLane = new BowlingLane(in.next().equals("1"), in.next().equals("1"), in.next().equals("1"),
                in.next().equals("1"), in.next().equals("1"));
        System.out.println((bowlingLane.one ? "!" : ".") + (bowlingLane.two ? "!" : ".") + (bowlingLane.three ? "!" : ".") +
                (bowlingLane.four ? "!" : ".") + (bowlingLane.five ? "!" : "."));
        classify(bowlingLane);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS