QUESTION 6
USING GUI:
IMPORT JAVAX.SWING.*;
PUBLIC CLASS NAMEOFYOURCLASS {
PUBLIC STATIC VOID MAIN (STRING[] ARGS) {
// TO READ USE: JOPTIONPANE.SHOWINPUTDIALOG("MESSAGE")
// TO WRITE USE:
// OPTIONPANE.SHOWMESSAGEDIALOG(NULL,STR,"MESSAGE",JOPTIONPANE.INFORMATION_MESSAGE);
}
}
AN ONLINE SYSTEM ALLOWS YOU TO BUY ICE CREAM. THE PRICES ARE:
$7 FOR 1 LITRE OF VANILLA OR CHOCOLATE.
$12 FOR 1 LITRE OF CARAMEL.
$14 FOR 1 LITRE OF THE SPECIAL OF THE DAY.
IF THE CUSTOMER ORDERS MORE THAN 5 LITRES OF ICE CREAM, THE CLIENT WILL RECEIVE A 10% DISCOUNT.
WRITE A JAVA PROGRAM THAT ASKS FOR THE QUANTITY (IN LITRES) OF ICE CREAM OF EACH TYPE, AND DISPLAYS THE TOTAL AMOUNT DUE. IF THE USER ENTERS A NEGATIVE NUMBER, THE PROGRAM SHOULD SHOW A MESSAGE AND ASK FOR THE INPUT AGAIN. YOU DO NOT NEED TO IMPLEMENT MULTIPLE CLASSES FOR THIS QUESTION. YOU CAN INCLUDE YOUR CODE WITHIN EITHER OF THE MAIN METHOD CONSTRUCTS AS SHOWN ABOVE.
FOR EXAMPLE, IF THE INPUT IS:
3 LITRES OF VANILLA ICECREAM
4 LITRES OF CARAMEL ICECREAM
THE TOTAL AMOUNT WILL BE 3*7+4*12 = 69. SINCE THE TOTAL IS MORE THAN 5 LITRES, THE AMOUNT WILL HAVE A 10% DISCOUNT. SO, THE FINAL AMOUNT IS 69*0.9 = 62.1
QUESTION 7
Write a java assignment program that asks a value n and x, and calculates and displays the result of the following expression. Your program should check if n is greater or equal to 1 and an odd number. If it is not, ask the user to input again. You don't need to check the value of x. Do not use any method from the Math class in your solution.
3x - 5x + 7x… nx
For example,
if n=7 and x=2, the result will be 32 - 52 + 72 = 33
if n=9 and x=2, the result will be 32 - 52 + 72 -92 = -48
QUESTION 8
Consider a class called Hotel. This class will have the instance variables:
name (String - name of the hotel
priceLow (double - price of the room with the cheapest rate)
priceHigh (double - price of the room with the highest rate).
The methods will be
setName (the parameter will be a String that will be stored in the variable name)
getName (it will return the name of the hotel)
setPriceLow (the parameters will be a double that will be stored in priceLow)
setPriceHigh (the parameters will be a double that will be stored in price high)
getPriceLow (it will return the priceLow)
getPriceHigh (it will return the price high)
All variables are private and all methods are public.
Write the implementation of this class in Java.
Question 9
Suppose now another class called BeachHotelCollection. Suppose the class BeachHotelCollection has the following declaration
Hotel p = new Hotel(“Hyatt”,300, 800);
Which is the name of the variable in the line above?
QUESTION 10
Continuing on from the previous question: Is it correct the statement p.priceLow =300;?
If it is not correct, write the correct statement to store 300 in priceLow.
Justify your answer.
QUESTION 11
Again, continuing on from the previous two questions:
Is it correct the statement System.out.println(p.getPriceHigh());?
If it is not correct, write the correct statement to print the price from p.
Solution:
Question 6:
import javax.swing.*;
public class Question6 {
public static void main(String[] args)
{
// Define costs here
int VAINILLA_PRICE = 7;
int CARAMEL_PRICE = 12;
int SPECIAL_PRICE = 14;
double DISCOUNT = 0.1; // discount to apply for total if liters higher than 5
while(true)
{
try
{
// ask for liters
int n_vainilla = Integer.valueOf((String)JOptionPane.showInputDialog(null, "Enter liters of Vainilla Icecream"));
int n_caramel = Integer.valueOf((String)JOptionPane.showInputDialog(null, "Enter liters of Caramel Icecream"));
int n_special = Integer.valueOf((String)JOptionPane.showInputDialog(null, "Enter liters of Special Of The Day Icecream"));
if(n_vainilla > 0 && n_caramel > 0 && n_special > 0)
{
// calculate sub-total
double subtotal = n_vainilla*VAINILLA_PRICE + n_caramel*CARAMEL_PRICE + n_special*SPECIAL_PRICE;
// check discount
int total_liters = n_vainilla + n_caramel + n_special;
double total = subtotal;
if(total_liters > 5)
total = total*(1.0-DISCOUNT);
// show result
String message = String.format("You ordered:\n\nVainilla Icecream: %d liters\nCaramel Icecream: %d liters\nSpecial of the day: %d liters\n\nSubtotal: $%.2f\nDiscount: $%.2f (%.0f)\nTotal: $%.2f", n_vainilla, n_caramel, n_special, subtotal, DISCOUNT*subtotal, DISCOUNT*100.0, total);
JOptionPane.showMessageDialog(null, message);
break;
}
else
JOptionPane.showMessageDialog(null, "Please enter positive values.");
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null, "Please enter valid numeric values.");
}
}
}
}
Question 7:
import javax.swing.*;
public class Question7 {
public static void main(String[] args)
{
try
{
int n = Integer.valueOf((String)JOptionPane.showInputDialog(null, "Enter n:"));
int x = Integer.valueOf((String)JOptionPane.showInputDialog(null, "Enter x:"));
if(n >= 1 && n%2 !=0) // n is positive and odd
{
int result = 0;
int ni = 3;
int i = 1;
String message = "The result is\n";
while(ni <= n)
{
result += Math.pow(-1, i+1)*Math.pow(ni, x);
if(Math.pow(-1, i+1) > 0) // positive
message += String.format("+%d^%d", ni, x);
else
message += String.format("-%d^%d", ni, x);
ni += 2;
i += 1;
}
// display result
message += String.format("=%d", result);
JOptionPane.showMessageDialog(null, message);
}
}
catch(Exception ex)
{
}
}
}
Question 8:
public class Hotel {
private String name;
private double priceLow;
private double priceHigh;
// constructor
public Hotel(String name, double priceLow, double priceHigh)
{
this.name = name;
this.priceLow = priceLow;
this.priceHigh = priceHigh;
}
// methods
public void setName(String newName) {name = newName;}
public String getName() {return name;}
public void setPriceLow(double newlow) {priceLow = newlow;}
public double getPriceLow() {return priceLow;}
public void setPriceHigh(double newhigh) {priceHigh = newhigh;}
public double getPriceHigh() {return priceHigh;}
}
Question 9.
The name of the variable is p
Question 10.
No, the statement is not correct because the variable priceLow is private and cannot be accessed, except via the public methods.
The correct way is:
p.setPriceLow(300);
Question 11.
Yes, the statement is correct. The piece of code is using the Hotel's public method
getPriceHigh()
Related Samples
At ProgrammingHomeworkHelp.com, we offer extensive support for Java assignments with our comprehensive collection of samples. Our Java assignment samples cover a range of topics from basic syntax to advanced concepts like multithreading and JavaFX. Whether you're tackling simple exercises or complex projects, our expertly crafted samples provide valuable insights and solutions. These resources are designed to help you understand key Java principles and enhance your coding skills. Explore our Java assignment samples today to get the support you need for academic success.
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java