Question

In: Computer Science

Using JAVA 2. A run is a sequence of adjacent repeated values. Write a code snippet...

Using JAVA

2. A run is a sequence of adjacent repeated values. Write a code snippet that generates a sequence of 20 random die tosses in an array and that prints the die values, marking the runs by including them in parentheses, like this:

1 2 (5 5) 3 1 2 4 3 (2 2 2 2) 3 6 (5 5) 6 (3 3)

Use the following pseudocode:

inRun = false

for each valid index i in the array

If inRun

If values [i] is different from the preceding value

Print )

inRun = false

If not inRun

If values[i] is the same as the following value

Print (

inRun = true

Print values[i]

//special processing to print last value

If inRun and last value == previous value, print  “ “ + value + “)”)

else if inRun and last value != previous value, print  “) “ + value )

else print “ “ + last value

3.

Implement a theater seating chart  as a two-dimensional array of ticket prices, like this:

{10, 10, 10, 10, 10, 10, 10, 10, 10, 10}

{10, 10, 10, 10, 10, 10, 10, 10, 10, 10}

{10, 10, 10, 10, 10, 10, 10, 10, 10, 10}

{10, 10, 20, 20, 20, 20, 20, 20, 10, 10}

{10, 10, 20, 20, 20, 20, 20, 20, 10, 10}

{10, 10, 20, 20, 20, 20, 20, 20, 10, 10}

{20, 20, 30, 30, 40, 40, 30, 30, 20, 20}

{20, 30, 30, 40, 50, 50, 40, 30, 30, 20}

{30, 40, 50, 50, 50, 50, 50, 50, 40, 30}

Write a code snippet that:

- uses a for loop to print the array with spaces between the seat prices

- prompts users to pick a row and a seat using a while loop and a sentinel to stop the loop.

- outputs the seat price to the user.

4. A pet shop wants to give a discount to its clients if they buy one or more pets and at least three other items. The discount is equal to 20 percent of the cost of the other items, but not the pets.

Write a program that prompts a cashier to enter each price and then a Y for a pet or N for another item. Use a price of –1 as a sentinel. Save the price inputs in a double(type) array and the Y or N in a corresponding boolean (type) array.

Output the number of items purchased, the number of pets purchased, the total sales amount before discount and the amount of the discount.

-----------------

Thank you so much!

Solutions

Expert Solution

2) JAVA CODE:

import java.util.*;


public class Run
{
public static void main(String[] args)
{
int i;
Random rand = new Random();
int[] Diee = new int[20];
for (i = 0; i < Diee.length; i++)
Diee[i] = rand.nextInt(6)+1;
boolean inRun = false;
  
for (i = 0; i < Diee.length-1; i++)
{
if(inRun)
{
if( i>0 && Diee[i]!=Diee[i-1])
{
System.out.print(")");
inRun = false;
}
else
System.out.print(" ");
}
if(!inRun)
{
if(i>0)
System.out.print(" ");
if(Diee[i]==Diee[i+1])
{
System.out.print("(");
inRun = true;
}
  
}
System.out.print(Diee[i]);   
  
}
if(inRun && Diee[Diee.length-1] == Diee[Diee.length-2])
{
System.out.print(" "+ Diee[Diee.length-1] + ")");
}
else if(inRun && Diee[Diee.length-1] != Diee[Diee.length-2])
{
System.out.print(")"+ Diee[Diee.length-1]);
}
else
{
System.out.print(" " + Diee[Diee.length-1]);
}
  
}
}

OUTPUT SCREENSHOT:

This is output for a single run and you can observe the random outputs for each run but which satisfies the question condition.

3)JAVA CODE:

import java.util.Scanner;

public class Theater_Seat_Seller {

// constants for the size of the theater

static final int NUM_ROWS = 9;

static final int NUM_COLS = 10;


int[] pricesAvailable = { 0, 0, 0, 0, 0 }; // 5 ticket prices, 10,20,30,40,50


int[][] seats = { { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 }, { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 },

{ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 }, { 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },

{ 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 }, { 10, 10, 20, 20, 20, 20, 20, 20, 10, 10 },

{ 20, 20, 30, 30, 40, 40, 30, 30, 20, 20 }, { 20, 30, 30, 40, 50, 50, 40, 30, 30, 20 },

{ 30, 40, 50, 50, 50, 50, 50, 50, 40, 30 } };

// constructor WHich you have to fill

public Theater_Seat_Seller() {

for (int i = 0; i < seats.length; ++i) {

for (int j = 0; j < seats[i].length; ++j) {

if (seats[i][j] == 10)

pricesAvailable[0]++;

else if (seats[i][j] == 20)

pricesAvailable[1]++;

else if (seats[i][j] == 30)

pricesAvailable[2]++;

else if (seats[i][j] == 40)

pricesAvailable[3]++;

else

pricesAvailable[4]++;

}

}

}


public void printSeats() {

System.out.println();

System.out.print(" ");


for (int i = 0; i < NUM_COLS; i++) {

System.out.printf("%5s", "[" + i + "]");

}

System.out.println(); // blank line

// iterate over rows

for (int i = 0; i < NUM_ROWS; i++) {

// print row heading

System.out.printf("%5s", "[" + i + "]");

// iterate over columns

for (int j = 0; j < NUM_COLS; j++) {

System.out.printf("%5d", seats[i][j]);

}

System.out.println(); // blank line

}

System.out.println("Tickets available by price: ");

System.out.println("$10: "+ pricesAvailable[0]);

System.out.println("$20: "+ pricesAvailable[1]);

System.out.println("$30: "+ pricesAvailable[2]);

System.out.println("$40: "+ pricesAvailable[3]);

System.out.println("$50: "+ pricesAvailable[4]);

}

public boolean getByPrice(int price) {

boolean retVal = false; // initially false, have not found

System.out.println("You chose to buy a ticket with price: $" + price);


for (int i = 0; i < seats.length; ++i) {

for (int j = 0; j < seats[i].length; ++j) {

if (seats[i][j] != 0 && seats[i][j] == price) {

if (seats[i][j] == 10)

pricesAvailable[0]--;

else if (seats[i][j] == 20)

pricesAvailable[1]--;

else if (seats[i][j] == 30)

pricesAvailable[2]--;

else if (seats[i][j] == 40)

pricesAvailable[3]--;

else

pricesAvailable[4]--;

  

seats[i][j] = 0;

retVal = true;

break;

}

  

}

if(retVal)

break;

}

return retVal; // return value

}

public boolean get_By_Location(int row, int col) {

boolean retVal = false; // initially false

System.out.println("You chose row: " + row + ", col: " + col);


if (!(row >=0 && row < seats.length) || !(col >= 0 && col < seats[0].length))

retVal = false;

else if (seats[row][col] == 0)

retVal = false;

else {

  

if (seats[row][col] == 10)

pricesAvailable[0]--;

else if (seats[row][col] == 20)

pricesAvailable[1]--;

else if (seats[row][col] == 30)

pricesAvailable[2]--;

else if (seats[row][col] == 40)

pricesAvailable[3]--;

else

pricesAvailable[4]--;

seats[row][col] = 0;

retVal = true;

}

return retVal; // return value

}


public void start() { // initiates the simulation

boolean running = true;

// Theater_Seat_Seller mySeller = new Theater_Seat_Seller();

String userInput = "";

Scanner myScanner = new Scanner(System.in);

// initial test to print the initial seating chart

printSeats();

System.out.println(); // blank line

System.out.println("Purchase by (P)rice or (L)ocation, P(R)INT, (Q)uit");

while (running) {// Theater_Seat_Seller execution loop

userInput = myScanner.next(); // get input

// set menu options here

// 1) quit

if (userInput.charAt(0) == 'Q' || userInput.charAt(0) == 'q') {

System.out.println("Good bye!!!");

break;

} else if (userInput.charAt(0) == 'P' || userInput.charAt(0) == 'p') {

System.out.println("You chose option: Price");

System.out.print("Enter Price (10,20,30,40,50):");

int price = 0;

userInput = myScanner.next();

System.out.println();

if (userInput.equals("10") || userInput.equals("20") || userInput.equals("30") || userInput.equals("40")

|| userInput.equals("50")) {

price = Integer.parseInt(userInput);

if (getByPrice(price)) {

System.out.println("Congratulations, you purchased a ticket for $" + price);

} else {

System.out.println("Sorry, no more tickets available for this price");

}

} else {// bad input

System.out.println("Invalid Entry.");

}

} else if (userInput.charAt(0) == 'L' || userInput.charAt(0) == 'l') {

System.out.println("You chose option: Location");

System.out.println("Enter Row and Column separated by a space:");

int row = -1;

int col = -1; // holds location

if (myScanner.hasNextInt()) {

row = myScanner.nextInt();

if (myScanner.hasNextInt()) {

col = myScanner.nextInt();

if (get_By_Location(row, col)) {

System.out.println("Congratulations, you purchased a ticket for at: " + row + ", " + col);

} else {

System.out.println("Sorry, no ticket available at this location.");

}

} else { // invalid, not an int

System.out.println("Invalid Entry, use Integer");

// flush input

userInput = myScanner.next();

}

} else {

System.out.println("Invalid Entry, use Integer");

// flush input

userInput = myScanner.next();

}

} else if (userInput.charAt(0) == 'R' || userInput.charAt(0) == 'r') {

printSeats();

} else {// error

System.out.println("Error, please choose P, L or Q");

}

// blank line before next prompt

System.out.println();

// prompt for next input

System.out.println("Purchase by (P)rice or (L)ocation, P(R)INT, (Q)uit");

}

myScanner.close(); // close resource when done.

}

public static void main(String[] args) {

// create a new Theater_Seat_Seller Object to run simulation

Theater_Seat_Seller mySeller = new Theater_Seat_Seller();

// start simulation

mySeller.start();

}

}

OUTPUT SCREENSHOT:

4)PYHTON CODE:

def discount(Prices, is_it_a_Pet, number_Of_Items):
i = 0
Cost_Of_Pet = 0
Pets = 0
Items = 0
Items_Cost = 0
while i < number_Of_Items:
if is_it_a_Pet[i]:
Cost_Of_Pet += Prices[i]
Pets += 1
else:
Items_Cost += Prices[i]
Items += 1
i += 1
if Pets >= 1 and Items >=5:
print("You receive discount")
discountAmount = 0.2 * Items_Cost
finalBill = Cost_Of_Pet + Items_Cost - discountAmount
print("Final Bill amount is", finalBill)
else:
print("You do not receive any discount")
finalBill = Cost_Of_Pet + Items_Cost
print("Final Bill amount is", finalBill)


Prices = []
is_it_a_Pet = []
while True:
price = int(input("Enter the price (-1 to quit): "))
if price != -1:
Prices.append(price)
choice = input("Is it a pet (Y / N)? ")
if choice == 'Y' or choice == 'y':
is_it_a_Pet.append(True)
else:
is_it_a_Pet.append(False)
print("")
else:
break
number_Of_Items = len(Prices)
discount(Prices, is_it_a_Pet, number_Of_Items)

*******It took me soo much time to solve this..I hope you find it helpful and please do UPVOTE as a favour,,,Thanks:)


Related Solutions

*****IN JAVA***** A run is a sequence of adjacent repeated values. Write a code snippet that...
*****IN JAVA***** A run is a sequence of adjacent repeated values. Write a code snippet that generates a sequence of 20 random die tosses in an array and that prints the die values, marking the runs by including them in parentheses, like this: 1 2 (5 5) 3 1 2 4 3 (2 2 2 2) 3 6 (5 5) 6 (3 3) Use the following pseudocode: inRun = false for each valid index i in the array If inRun...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then prints the following output: a. every element (on a single line) b. every element at an even index (on a single line) c. every even element (on a single line) d. all elements in reverse order (on a single line) e. only the first and last elements (on a single line)
Write a java code snippet to prompt the user for the number of names they’d like...
Write a java code snippet to prompt the user for the number of names they’d like to enter. Create a new array of the size chosen by the user and prompt the user for each of the names. Output the list of names in reverse order.
Write a java code snippet that allows a teacher to track her students’ grades. Use a...
Write a java code snippet that allows a teacher to track her students’ grades. Use a loop to prompt the user for each student’s name and the grade they received. Add these values to two separate parallel ArrayLists. Use a sentinel to stop. Output a table listing each of the student’s names in one column and their associated grades in the second column.
please write the java code so it can run on jGRASP Thanks! 1 /** 2 *...
please write the java code so it can run on jGRASP Thanks! 1 /** 2 * PassArray 3 * @Sherri Vaseashta 4 * @Version 1 5 * @see 6 */ 7 import java.util.Scanner; 8 9 /** 10 This program demonstrates passing an array 11 as an argument to a method 12 */13 14 public class PassArray 15 { 16 public static void main(String[] args) 17 { 18 19 final int ARRAY_SIZE = 4; //Size of the array 20 // Create...
please write the java code so it can run on jGRASP Thanks! CODE 1 1 /**...
please write the java code so it can run on jGRASP Thanks! CODE 1 1 /** 2 * SameArray2.java 3 * @author Sherri Vaseashta4 * @version1 5 * @see 6 */ 7 import java.util.Scanner;8 public class SameArray29{ 10 public static void main(String[] args) 11 { 12 int[] array1 = {2, 4, 6, 8, 10}; 13 int[] array2 = new int[5]; //initializing array2 14 15 //copies the content of array1 and array2 16 for (int arrayCounter = 0; arrayCounter < 5;...
2. Translate the following C/Java code to MIPS assembly code. Assume that the values of a,...
2. Translate the following C/Java code to MIPS assembly code. Assume that the values of a, i, and j are in registers $s0, $t0, and $t1, respectively. Assume that register $s2 holds the base address of the array A (add comments to your MIPS code). j = 0; for(i=0 ; i<a ; i++) A[i]=i+j++;
Write a code snippet for the following:   You need to control the maximum number of people...
Write a code snippet for the following:   You need to control the maximum number of people who can be in a   restaurant at any given time. A group cannot enter the restaurant if they   would make the number of people exceed 100 occupants. Use random numbers   between 1 and 20 to simulate groups arriving to the restaurant. After each   random number, display the size of the group trying to enter and the number   of current occupants. As soon as the...
Genetic Code: Using the standard genetic code, write a sequence encoding the peptide "MASTERMIX" How many...
Genetic Code: Using the standard genetic code, write a sequence encoding the peptide "MASTERMIX" How many different sequences can encode this peptide? How many genetic codes have been described? (Hint: search NCBI Genetic Codes) How, in a general way, do these alternative codes tend to differ from the standard genetic code? How is selenocysteine encoded?
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester. The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT