In: Computer Science
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!
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:)