Assignment 

Instruction(s):

I would like to see solutions for these JAVA assignments in a format that can either be loaded into Eclipse, or in text format.  A test file is also needed for each one

Lab3B.txt 

INTRODUCTION

Given pennies, convert them to dollars, quarters, dimes, nickels and pennies

Details

Write an application that prompts user to enter pennies. It then converts these pennies into number of dollars, quarters, dimes, nickels and pennies.

Expected Output:

Please enter pennies: 141

Original amount: 141

Number of Dollars: 1

Number of Quarters:  1

Number of dimes:  1

Number of Nickels:  1

Number of Pennies:  1 

Lab5.txt

Lab Assignment

Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables.

A part number (type String)

A part description (type String)

A quantity of the item being purchased (type int)

A price per item (double)

Your class should have a constructor that initializes the four instance variables. Provide a set and a get method for each instance variable. In addition, provide a method named getInvoiceAmount that calculates the invoice amount (i.e., multiplies the quantity by the price per item), then returns the amount as a double value. If the quantity is not positive, it should be set to 0. If the price per item is not positive, it should be set to 0.0. Write a test application named InvoiceTest that demonstrates class Invoice’s capabilities.

Hints:

  • The input values for the quantity and the price per item must be validated before they can be used to set the corresponding instance variables.

This should be done both in the constructor and in the appropriate set methods.

  • You create an object and then modify that oblect
  • The method header for getInvoiceAmount should be

public double getInvoiceAmount().

Class InvoiceTest’s logic should be as follows:

Create a new Invoice object with the following arguments:

“1234”, “Hammer”, 2, 14.95

Display text “Original invoice information” and then display information about the invoice.

Then use set Methods to update invoice information and display it again.

Set Part Number to “001234″

Set Part Description to “Yellow Hammer”

Set Quantity to 3

Set Price per Item to 19.49

// For-validating negative quantify and price

Set Part Number to “001234″

Set Part Description to “Yellow Hammer”

Set Quantity to – 3

Set Price per Item to -19.49

Lab7.txt

Introduction

Write a Java console application that simulates a game of craps. The game should be in a separate class from the main program.

Details:

Write a program that simulates a game of craps. The rules of the game as well as the game logic are provided below.

Game Rules:

A player rolls two dice. Each die has six faces. These faces contain 1, 2, 3, 4, 5 and 6 spots. After the dice have come to rest, the sum of the spots on the two upward faces is calculated.

If the sum is 7 or 11 on the first roll, the player wins.

If the sum is 2, 3 or 12 on the first roll (called “craps”), the player loses.

If the sum is 4, 5, 6, 8, 9 or 10 on the first roll, then that sum becomes the player’s “point” and then the game continues.  To win, you must continue rolling the dice until you “make your point.” The player loses by rolling a 7 before making the point.

Game Logic:

Roll two die and read their values. The sum of the values from two die must be between 2 and 12.

Using a switch statement, check to see if user won, lost or the game continues.

If game continues, roll the dice until the game is won (player gets his point) or lost (player rolled a 7).

Display a message to the player that he/she won or lost the game.

Requirements

There are very specific requirements for this program. The applications must contain 3 files.

  1. Game Class
  2. GameTester or main application class
  3. A class for Enums
  4. Must use a switch statement to check the status of the game after the first roll of die.
  5. Must use a function for rolling the dice. This can be part of the Game class

Lab8.txt

 Introduction

Write an interactive console application in Java that plays a guessing game program with the user.

Details:

For the Console Application:

Write a program that prompts the user: Do want to play the Guessing Game?

If user says “Yes”, the computer generates a random number between 1 and 1000 and saves it. User is expected to guess that number. When user enters a value, tell the

user if the guess is too high or too low. If the user enters a -1, terminate the game with a message: Thanks for playing the guessing game. Good bye….

If user guesses that number in less than 10 tries, the computer responds:

Congratulations!!! You won the game in n tries. You did very well.

If user guesses that number in 10 tries or more tries, the computer responds:

Congratulations!!! You won the game in nn tries. You could do better. 

Solution 

Lab3B.java 

package lab3_B;

importjava.util.Scanner;

public class Lab3B {

public static void main (String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println(“Please enter pennies:”);

int amount=sc.nextInt();

System.out.println(“Original amount: “+amount);

int Dollars = amount / 100;

amount = amount % 100;

System.out.println(“Number of Dollars: “+Dollars );

int Quarters = amount / 25;

amount = amount % 25;

System.out.println(“Number of Quarters: “+Quarters );

int dimes = amount / 10;

amount = amount % 10;

System.out.println(“Number of dimes: “+dimes );

int Nickels = amount / 5;

amount = amount % 5;

System.out.println(“Number of Nickels: “+Nickels );

int Pennies = amount;

System.out.println(“Number of Pennies: “+Pennies );

}

} 

Lab5 

Invoice.java

importjava.lang.String;

public class Invoice {

private String part_number;

private String part_description;

privateint quantity;

private double price;

Invoice(String part_number, String part_description, int Quantity, double Price)

{

Setpartnumber(part_number);

SetPartdescription(part_description);

Setquantity(Quantity);

Setprice(Price);

}

public void SetPartdescription(String part_description) {

System.out.println(“Set Part Description to: ” + part_description);

this.part_description = part_description;

}

public void Setpartnumber(String part_number) {

System.out.println(“Set Part Number to: ” + part_number);

this.part_number = part_number;

}

public void Setprice(double d) {

// TODO Auto-generated method stub

System.out.println(“Set Part Price to: ” + d);

this.price = d;

}

public void Setquantity(int i) {

// TODO Auto-generated method stub

System.out.println(“Set Part Quantity to: ” + i);

this.quantity = i;

}

public String getPartdescription() {

returnthis.part_description;

}

public String getpartnumber() {

returnthis.part_number ;

}

public double getInvoiceAmount()

{

doubleinvoiceamount = this.getquantity() * this.getprice();

returninvoiceamount;

}

public void updateInfo()

{

if(this.quantity< 0)

Setquantity(0);

if(this.price< 0)

Setprice(0.0);

}

public double getprice() {

// TODO Auto-generated method stub

returnthis.price ;

}

publicintgetquantity() {

// TODO Auto-generated method stub

returnthis.quantity;

}

} 

TestInvoice.java

importjava.lang.String;

public class TestInvoice {

public static void main(String args[]){

String part_number1 = “001234”;

String part_description = “Yellow Hammer”;

int Quantity = -3;

double Price = -19.49;

Invoice I = new Invoice(part_number1,part_description,Quantity, Price);

System.out.println(“Original invoice information”);

System.out.println(“Part Number : “+ I.getpartnumber());

System.out.println(“Part Description : “+I.getPartdescription());

System.out.println(“Quantity: “+ I.getquantity());

System.out.println(“Price: “+ I.getprice());

System.out.println(“Invoice Amount: “+ I.getInvoiceAmount());

I.updateInfo();

System.out.println(“Updated invoice information”);

System.out.println(“Part Number : “+ I.getpartnumber());

System.out.println(“Part Description : “+I.getPartdescription());

System.out.println(“Quantity: “+ I.getquantity());

System.out.println(“Price: “+ I.getprice());

System.out.println(“Invoice Amount: “+ I.getInvoiceAmount());

}

} 

Lab7 

Enums.java

public class Enums {

publicenum Status { CONT, WON, LOST };

// constants that represent common rolls of the dice

public final static int two = 2;

public final static int three = 3;

public final static int seven = 7;

public final static int eleven = 11;

public final static int twelve = 12;

} 

Game.java

importjava.util.Random;

importjava.util.Scanner;

importjava.lang.*;

public class Game

{

// create random number generator for use in method rollDice

private Random randomNumbers = new Random();

// enumeration with constants that represent the game status

//private enum Status { CONTINUE, WON, LOST };

// plays one game of craps

public void play()

{

Enums e1 = new Enums();

inttotalPoint = 0; // point if no win or loss on first roll

Enums.StatusgameStatus; // can contain CONTINUE, WON or LOST

intdiceSum = rollDice(); // first roll of the dice

// determine game status and point based on first roll

switch ( diceSum )

{

caseEnums.seven: // win with 7 on first roll

caseEnums.eleven: // win with 11 on first roll

gameStatus = Enums.Status.WON;

break;

caseEnums.two: // lose with 2 on first roll

caseEnums.three: // lose with 3 on first roll

caseEnums.twelve: // lose with 12 on first roll

gameStatus = Enums.Status.LOST;

break;

default: // did not win or lose, so remember point

gameStatus = Enums.Status.CONT; // game is not over

totalPoint = diceSum; // remember the point

System.out.printf( “Point is %d\n”, totalPoint );

break; // optional at end of switch

} // end switch

// while game is not complete

while ( gameStatus == Enums.Status.CONT ) // not WON or LOST

{

diceSum = rollDice(); // roll dice again

// determine game status

if ( diceSum == totalPoint ) // win by making point

gameStatus = Enums.Status.WON;

else

if ( diceSum == Enums.seven ) // lose by rolling 7 before point

gameStatus = Enums.Status.LOST;

} // end while

// display won or lost message

if ( gameStatus == Enums.Status.WON )

System.out.println( “Player wins…” );

else

System.out.println( “Player loses…” );

} // end method play

// roll dice, calculate sum and display results

publicintrollDice()

{

// pick random die values

int die1 = 1 + randomNumbers.nextInt( 6 ); // first die roll

int die2 = 1 + randomNumbers.nextInt( 6 ); // second die roll

int sum = die1 + die2; // sum of die values

// display results of this roll

System.out.println( “Player rolled: “+ sum +”:     “+

die1+”,  “+ die2);

return sum; // return sum of dice

} // end method rollDice

} // end class Craps 

GameTester.java 

importjava.util.Scanner;

public class GameTester {

public static void main(String args[]){

Game c = new Game();

System.out.println(“Game has started\nWould you like to play a game of Craps? [Y|N]:”);

Scanner sc=new Scanner(System.in);

String input=sc.next();

while(input.equals(“y”) || input.equals(“Y” ) ){

c.play();

System.out.println(“Would you like to play a game of Craps? [Y|N]:”);

input=sc.next();

}

}

}