Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Consider the below given code: // importing required Scanner class import java.util.Scanner; // main class public class Main { // discount method public static void

Consider the below given code:

// importing required Scanner class

import java.util.Scanner;

// main class

public class Main

{

// discount method

public static void discount(double[] prices, boolean[] isPet, int nItems)

{

// if number of items is atleast 5

if(nItems >= 5)

{

// declare required variables

boolean pet = false;

// for loop

for(int i = 0;i

{

// if block

if(isPet[i] == true)

{

// assign true to variable

pet = true;

// use break statement

break;

}

}

// for loop

for(int i = 0;i

{

// if block

if(isPet[i] == false)

{

// calculate discount

prices[i] = prices[i] * 0.8;

}

}

}

}

// main method

public static void main(String args[])

{

// declaring required variables

int n;

// instantiate Scanner class

Scanner scanner = new Scanner(System.in);

// display message on console

System.out.print("Enter number of items: ");

// reading input from the user

n = scanner.nextInt();

// declaring array

double price[] = new double[n];

boolean isPet[] = new boolean[n];

// declare String

String temp;

// for loop

for(int i = 0;i

{

// display message on console

System.out.println("Enter price of item"+(i+1)+": ");

// store data in array

price[i] = scanner.nextDouble();

// display data on console

System.out.print("Is this item is a pet? (y/n): ");

//read input from the user

temp = scanner.next().toLowerCase();

// if inserted character value is y

if(temp.charAt(0) == 'y')

{

// assign true to array

isPet[i] = true;

}

// otherwise

else

{

//assign false to array

isPet[i] = false;

}

}

// calling discount method

discount(price,isPet,n);

System.out.println("Data with discount: ");

// for loop

for(int i = 0;i

{

// display data on console

System.out.println("Rate for item number " + i + " : " +price[i]);

}

}

}

Simply modify the provided code with the sentinel price value -1. In more clear terms use while loop, that will check the user entered price value if the price value is -1, then simply terminate the program, else continue the processing.

provide a valid output sample, alongwith running code snips and code to copy.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions