Use JGrasp to design and implement the following programs (from the end of Chapt
ID: 3861746 • Letter: U
Question
Use JGrasp to design and implement the following programs (from the end of Chapter 6 in the textbook) and understand what they do. Each program includes a main method and the specified method(s) in the problem statement. Notice that the main method is used to test the specified method(s). Required: Use java assert statement to validate program input values when conditions applied to inputs (for int and char types). See Chapter 3 slides for code examples. Apply this requirement to both lab exercises and assignment programs. Design and implement a Java program for programming exercise 6.7, page 235, from the textbook (name it InvestmentValue). Implement the main method as stated in the problems statement. Sample output is given in the textbook. Document your code, and organize the outputs as shown in the sample output using escape characters. Design and implement a Java program for programming exercise 6.15, page 237, from the textbook (name it TaxTable). Design the program to print out separate tables for filing status (Single, Married Joint or Qualifying Widow(er), Married Separate, and Head of house). Document your code, and organize the outputs use escape characters. Design and implement two Java programs for programming exercise 6.19, page 238. The first program (called My Triangle) is to implement the specified methods. The second program (called TestMyTriangle) is to test the first program methods. Program TestMyTriangle is used to compute the area of a triangle if the input is valid. Notice that method isvalid () is used to validate the input before attempting to compute the area. See listings 10 and 11 (page 224) on how to write 2 programs (main program and test program). Design the test program main method such that it allows the user to re-run the program with different inputs ((i.e., use a loop structure). Document your code and organize the outputs properly using escape. Save both programs in the same folder. Design and implement a Java program for programming exercise 6.30, page 241, from the textbook (name it CrapsGame). Rolling a dice is simply generating a random integer number between 1 and 6. Write a separate method for this function, call it rollDie() to return the generated number (one number between 1 and 6). Do NOT print anything inside this method. Follow the game rules stated in the problem statement. Use escape characters to format the outputs as shown in the sample outputs.Explanation / Answer
GameOfCraps.java
import java.util.Scanner;
public class GameOfCraps {
public static void main(String[] args) {
// Scanner class object is used to read the numbers entered by the user
Scanner sc = new Scanner(System.in);
// Declaring variables
int result;
// Calling the method craps()
result = craps();
// if the result value is zero it displays the message "You Lost"
if (result == 0) {
System.out.println("You lost.");
}
// if the result value is one it displays the message "You Won"
else if (result == 1) {
System.out.println("You won!");
}
}
private static int craps() {
// Declaring variables
int count = 0, point = 0, num, dice1, dice2;
// This while loop continue to execute either player wins or lose the
// game
while (true) {
dice1 = rollDie();
dice2 = rollDie();
// calculating the sum of two dice values
num = dice1 + dice2;
/*
* If the player got sum as 7 for the first time player wins else
* player lost
*/
if (num == 7 || num == 11) {
if (count == 0) {
System.out.println("Roll :" + dice1 + " + " + dice2 + "="+ num);
return 1;
} else if (num == 7) {
System.out.println("Roll :" + dice1 + " + " + dice2 + "="+ num);
return 0;
} else if (num == 11) {
System.out.println("Roll :" + dice1 + " + " + dice2 + "="+ num);
count++;
continue;
}
}
/*
* If the player got sum as 2 or 3 or 12 for the first time player
* wins else player continue the game
*/
else if (num == 2 || num == 3 || num == 12) {
if (count == 0) {
System.out.println("Roll :" + dice1 + " + " + dice2 + "="+ num);
return 0;
} else {
System.out.println("Roll :" + dice1 + " + " + dice2 + "="+ num);
count++;
continue;
}
}
/*
* If the player rolled the dice and got the number which is same as
* point Player won the game
*/
else if (point == num) {
System.out.println("Roll :" + dice1 + " + " + dice2 + "=" + num);
return 1;
}
/*
* If the player roll the dice and got sum other than 7 for the
* first time The point is set.
*/
else {
if (count == 0) {
System.out.println("Roll :" + dice1 + " + " + dice2 + "="+ num);
point = num;
System.out.println("Point is " + point);
count++;
continue;
} else {
System.out.println("Roll :" + dice1 + " + " + dice2 + "="+ num);
count++;
continue;
}
}
}
}
private static int rollDie() {
// Generating the random number between 1 and 6
return (int) (6.0 * Math.random() + 1.0);
}
}
___________________
Output#1:
Roll :2 + 3=5
Point is 5
Roll :5 + 2=7
You lost.
_________________
Output#2:
Roll :2 + 2=4
Point is 4
Roll :4 + 6=10
Roll :1 + 6=7
You lost.
_______________
Output#3:
Roll :5 + 1=6
Point is 6
Roll :5 + 2=7
You lost.
_________________
Output#4:
Roll :6 + 2=8
Point is 8
Roll :4 + 6=10
Roll :3 + 1=4
Roll :4 + 4=8
You won!
______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.