Running a Java file from Eclipse on Mac - java

I'm writing a simple Java program for my history final project and am having trouble exporting it out of eclipse so it can be run on my teacher's computer (a Mac).
Here is the code:
package project;
import java.util.Scanner;
public class Denim {
public static void main(String []args) {
Scanner scan = new Scanner(System.in);
System.out.println("What item of denim is in question?");
String str = scan.nextLine();
String str1 = str.toUpperCase();
String jeans = "JEANS";
String jeansp = "JEAN PANTS";
String tux = "CANADIAN TUXEDO";
String tux1 = "CANADIEN TUXEDO";
String tux2 = "CANADIAN TUX";
String tux3 = "CANADIEN TUX";
String jacket = "JACKET";
String jjacket = "JEAN JACKET";
String hat = "HAT";
String dhat = "DENIM BASEBALL HAT";
String bhat = "BASEBALL HAT";
String c = "C";
int jeanFactor = 9982;
double jacketFactor = 10466.25178;
double bottleFactor = 128/16.9;
double hatFactor = 314.1415;
if (str1.equals(jeans) || str1.equals(jeansp)){
System.out.println("How many pairs of jeans?");
int numj = scan.nextInt();
int gallonj = numj*jeanFactor;
System.out.println("Producing " + numj + " pairs of jeans would use: ");
System.out.println(gallonj + " gallons of water");
double bottlesj = gallonj*bottleFactor;
System.out.println(bottlesj + " bottles of water");}
else if ((str1.equals(tux)) || (str1.equals(tux1)) || (str1.equals(tux2)) ||(str.equals(tux3))){
System.out.println("How many tuxedos?");
int numt = scan.nextInt();
int gallontp = numt * jeanFactor;
double gallontj = numt * jacketFactor;
double gallont = gallontp + gallontj;
double bottlest = gallont*bottleFactor;
System.out.println("Producing " + numt +" " + c + str.substring(1,8) + " tuexedos would use: ");
System.out.println(gallont +" gallons of water.");
System.out.println(bottlest + " bottles of water");}
else if (str1.equals(jacket) || str.equals(jjacket)){
System.out.println("How many jackets?");
int numjj = scan.nextInt();
double gallonjj = numjj*jacketFactor;
double bottlesjj = numjj*bottleFactor;
System.out.println("Producing " + numjj + " jackets would use: ");
System.out.println(gallonjj + " gallons of water");
System.out.println(bottlesjj + " bottles of water");}
else if (str1.equals(hat) || str.equals(dhat) || str.equals(bhat)){
System.out.println("How many hats?");
int numh = scan.nextInt();
double gallonh = numh*hatFactor;
double bottlesh = numh*bottleFactor;
System.out.println("Producing " + numh + " jackets would use: ");
System.out.println(gallonh + " gallons of water");
System.out.println(bottlesh + " bottles of water");
}
}
}
I click file-export and export it as a .jar file, but every time I try and open it to run it, a message pops up saying "The Java JAR file could not be launched. Does anyone know how to fix this or what I'm doing wrong? Both my computer and my teacher's are Macbooks.

Related

Adding more classes to a prewritten program

I am being tasked with adding 2 more classes to a prewritten program, one that does all calculations and one that prints the total bill. I understand how to add other classes but im a bit confused since the program already looks like it does the calculations inside of the main class.
Ive tried just adding in the classes but it throws errors because its missing information obviously.
public static void main(String[] args)
{
String squantity, snumber, output, line_output = "";
String [] item = new String [5];
double [] cost = new double [5];
double [] quantity = new double [5];
double [] amount = new double [5];
int number, i;
double grandtotal = 0;
String costout, amountout, grandtotalout;
DecimalFormat df2 = new DecimalFormat("$##,###.00");
for(i=0;i<=4;++i)
{
output = " Acme Grocery Store" + "\n" +
"1-Green Beans $0.35 per pound" + "\n" +
"2-Yellow Beans $0.40 per pound" + "\n" +
"3-Head Lettuce $0.79 per pound" + "\n" +
"4-Leaf Lettuce $1.98 per pound" + "\n" +
"5-Hot House Tomatoes $0.99 per pound" + "\n" +
"6-Hydro Tomatoes $3.98 per pound" + "\n" + "\n" +
"Please make your selection ";
snumber = JOptionPane.showInputDialog(null,
output, "Input Data", JOptionPane.QUESTION_MESSAGE);
number = Integer.parseInt(snumber);
squantity = JOptionPane.showInputDialog(null,
"Enter Quantity", "Input Data", JOptionPane.QUESTION_MESSAGE);
quantity[i] = Double.parseDouble(squantity);
//code for the calculation
if(number == 1)
{
cost[i] = 0.35;
item[i]="Green Beans";
}
else if(number == 2)
{
cost[i] = 0.4;
item[i]="Yellow Beans";
}
else if(number == 3)
{
cost[i] = 0.79;
item[i]="Head Lettuce";
}
else if(number ==4)
{
cost[i] = 1.98;
item[i]="Leaf Lettuce";
}
else if (number==5)
{
cost[i] = 0.99;
item[i]="Hot House Tomatoes";
}
else
{
cost[i] = 3.98;
item[i]="Hydro Tomatoes";
}
amount[i]=cost[i]*quantity[i];
costout=df2.format(cost[i]);
amountout=df2.format(amount[i]);
line_output=line_output+item[i]+" "+costout+" "+amountout+"\n";
grandtotal=grandtotal + amount[i];
}//for loop
grandtotalout=df2.format(grandtotal);
output=line_output+"\n"+ "The total grocery bill = "+grandtotalout;
JOptionPane.showMessageDialog(null, output, " ", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}//main
We are expected to add the classes
class grocery {
}
class printbill{
}
I tried messing with the extends function but I dont think that is correct either
As a starter, your grocery class would look something like this:
final class Grocery
{
Grocery(String InputItem, double InputCost, double InputQuantity)
{
Item = InputItem;
Cost = InputCost;
Quantity = InputQuantity;
}
public final String GetItem()
{
return Item;
}
public final double GetCost()
{
return (double)InputQuantity * Cost;
}
private final String Item;
private final double Cost;
private final int Quantity;
}
The PrintBill class may go like this:
final class PrintBill
{
PrintBill(Grocery [] InputGroceries)
{
Groceries = InputGroceries;
}
public final void Print()
{
double Cost, TotalCost = 0.0;
String Line = "";
String Item;
for (int i = 0; i < Groceries.length; i++)
{
Cost = Groceries[i].GetCost();
Item = Groceries[i].GetItem();
TotalCost += Cost;
// I'll leave it up to you to print this info out.
Line = Item + " " + Cost +" "+ TotalCost +"\n";
}
JOptionPane.showMessageDialog(null, output, " ",
JOptionPane.INFORMATION_MESSAGE);
}
private final Grocery [] Groceries;
}
Your main() would then be:
public static void main(String[] args)
{
String squantity, snumber, output, line_output = "";
Grocery [] Groceries = new Grocery [4];
PrintBill TheBill;
int number;
for (int i = 0; i < Groceries.length; i++)
{
output = " Acme Grocery Store" + "\n" +
"1-Green Beans $0.35 per pound" + "\n" +
"2-Yellow Beans $0.40 per pound" + "\n" +
"3-Head Lettuce $0.79 per pound" + "\n" +
"4-Leaf Lettuce $1.98 per pound" + "\n" +
"5-Hot House Tomatoes $0.99 per pound" + "\n" +
"6-Hydro Tomatoes $3.98 per pound" + "\n" + "\n" +
"Please make your selection ";
snumber = JOptionPane.showInputDialog(null,
output, "Input Data", JOptionPane.QUESTION_MESSAGE);
number = Integer.parseInt(snumber);
squantity = JOptionPane.showInputDialog(null,
"Enter Quantity", "Input Data", JOptionPane.QUESTION_MESSAGE);
quantity = Double.parseDouble(squantity);
if(number == 1)
{
Groceries[i] = new Grocery("Green Beans", 0.35, quantity);
}
else if(number == 2)
{
Groceries[i] = new Grocery("Yellow Beans", 0.4, quantity);
}
// etc...
}
TheBill = new PrintBill(Groceries);
TheBill.Print();
}
More work is required to actually complete this but it should give you a starting point anyway.
Happy Days :-)

Shopping cart almost working

Am unable to still get this program working like it should. Most of it works, see code below:
import java.io.*;
import java.util.*;
public class demo {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
String str[] = { "Bananas", "Apples", "Oranges" };
double price[] = { 2.09, 3.99, 2.19};
int i = 0;
int j = 0;
System.out.print("Enter type of product: ");
String string = sc.next();
if ("fruit".equals(string)) {
while (i < str.length) {
while (j < price.length) {
System.out.print(str[i++] + ": " + "£" + (price[j++]) + "p per bag \n");
}
}
}
System.out.print("\n");
System.out.print("Enter which type of " + string + ": ");
String string1 = sc.next();
boolean strs = "bananas".equals(string1);
boolean strs1 = "apples".equals(string1);
boolean strs2 = "oranges".equals(string1);
if (strs){
System.out.print("Enter qty of " + str[0] + " (by bag): ");
}
if (strs1) {
System.out.print("Enter qty of " + str[1] + " (by bag): ");
}
if (strs2) {
System.out.print("Enter qty of " + str[2] + " (by bag): ");
}
int qty = sc.nextInt();
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int h = 1;
int[] g = {0,1,2};
if ((a[h] == (qty)) || (strs) || (strs1) || (strs2)) {
System.out.print("\n"+ qty + " bag(s) of " + string1 + " have been added to your basket, "
+ "total costing £"+ (qty) * (price[g[0]]) + "p");
}
}
}
How can I get this code to work...any ideas?(ie the type of product(fruit), the type of fruit(bananas or apples or oranges), and the qty of them (between 1-10), but when it comes to working out Total cost, it comes out wrong...
One logic is this.
double itemCost = 0;
if (strs){
System.out.print("Enter qty of " + str[0] + " (by bag): ");
itemCost = price[0];
}
if (strs1) {
System.out.print("Enter qty of " + str[1] + " (by bag): ");
itemCost = price[1];
}
if (strs2) {
System.out.print("Enter qty of " + str[2] + " (by bag): ");
itemCost = price[2];
}
int qty = sc.nextInt();
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int h = 1;
int[] g = {0,1,2};
if ((a[h] == (qty)) || (strs) || (strs1) || (strs2)) {
System.out.print("\n"+ qty + " bag(s) of " + string1 + " have been added to your basket, "
+ "total costing £"+ (qty) * (itemCost) + "p");

How to create number limitations with JOptionPane.showInputDialog(null, "");

So I'm making a game as you can see but i need to make it so that the player cannot use more than 28 stat points.
Can anyone help?
Here is my code.
package fallout.pkg4.game;
import javax.swing.JOptionPane;
public class Fallout4Game
{
public static void main(String[] args)
{
int pointsint2;
int pointsint;
String points;
String points2;
int resultint;
String perc;
int resultPerc;
int result3;
String end;
int endParsed;
int result4;
String chari;
int chariParsed;
int result5;
String inte;
int inteParsed;
int result6;
String agi;
int agiParsed;
int result7;
String luck;
int luckParsed;
Boolean error;
points = JOptionPane.showInputDialog(null, "Welcome to Brandons first game. Please enter 28 to start.");
JOptionPane.showMessageDialog(null,"You have " + points + " stats points. Choose wisley.....");
JOptionPane.showMessageDialog(null,"You will be choosing your S.P.E.C.I.A.L. attributes");
points2 = JOptionPane.showInputDialog(null, "STRENGTH You have " + points + " points.");
if(JOptionInput <= then )
pointsint = Integer.parseInt(points);
pointsint2 = Integer.parseInt(points2);
resultint = pointsint - pointsint2;
perc = JOptionPane.showInputDialog(null,"PERCEPTION You have " + resultint + " points left");
resultPerc = Integer.parseInt(perc);
result3 = resultint - resultPerc;
end = JOptionPane.showInputDialog(null,"ENDURENCE You have " + result3 + " points left.");
endParsed = Integer.parseInt(end);
result4 = result3 - endParsed;
chari = JOptionPane.showInputDialog(null,"CHARISMA You have " + result4 + " points left.");
chariParsed = Integer.parseInt(chari);
result5 = result4 - chariParsed;
inte = JOptionPane.showInputDialog(null,"INTELIGENCE You have " + result5 + " points left.");
inteParsed = Integer.parseInt(inte);
result6 = result5 - inteParsed;
agi = JOptionPane.showInputDialog(null,"AGILITY You have " + result6 + " points left");
agiParsed = Integer.parseInt(agi);
result7 = agiParsed - result6;
luck = JOptionPane.showInputDialog(null,"LUCK You have " + result7 + " points left");
luckParsed = Integer.parseInt(luck);
}
}

Converting output to include discounts when applicable

Here's the code I have so far
import java.util.Scanner;
import java.text.DecimalFormat;
import java.util.Random;
public class TicketInfo
{
static final double STUDENT_DISCOUNT = .80;
static final double FACULTY_STAFF_DISCOUNT = .20;
/**
*Prints date, time, section, row, seat,
*price, cost, final cost with discount.
*
*#param args Command line arguements (not used).
*/
public static void main(String[] args)
{
Scanner userInput = new Scanner(System.in);
DecimalFormat fmt = new DecimalFormat("$#,##0.00");
DecimalFormat form = new DecimalFormat("###");
String ticketCode = "";
String event = "";
String date = "";
String time = "";
String section = "";
String row = "";
String seat = "";
String price = "";
String type = "";
String cost = "";
int section1, row1, seat1;
double price1, cost1;
Random generator = new Random();
int random;
System.out.print("Enter your ticket code: ");
ticketCode = userInput.nextLine();
System.out.println();
//Trims any extra white spaces.
ticketCode = ticketCode.trim();
if (ticketCode.length() > 27)
{
//Breaks down the ticket code into parts
type = ticketCode.substring(0, 3);
date = ticketCode.substring(14, 16) + "/"
+ ticketCode.substring(16, 18)
+ "/" + ticketCode.substring(18, 22);
time = ticketCode.substring(22, 24)
+ ":"
+ ticketCode.substring(24, 26);
section = ticketCode.substring(4, 5);
row = ticketCode.substring(5, 7);
seat = ticketCode.substring(8, 9);
price = ticketCode.substring(9, 12);
event = ticketCode.substring(26, ticketCode.length());
cost = ticketCode.substring(10, 14);
//Converts Doubles or Integers in a string into its numeric value.
section1 = Integer.parseInt(section);
row1 = Integer.parseInt(row);
seat1 = Integer.parseInt(seat);
price1 = Double.parseDouble(price);
cost1 = Double.parseDouble(cost);
//Calculate cost based on ticket type
random = generator.nextInt(999999) + 1;
// Print results
System.out.println("Event: " + event + " Date: "
+ date + " Time: " + time);
System.out.println("Section: " + form.format(section1) + " Row: "
+ form.format(row1) + " Seat: " + form.format(seat1));
System.out.println("Price: " + fmt.format(price1) + " Ticket Type: "
+ type + " Cost: " + fmt.format(price1));
System.out.println("Raffle Number: " + random);
}
else
{
System.out.println("Invalid Ticket Code.");
System.out.println("Ticket code must have at least 27 characters.");
}
}
}
Output as of now:
Enter your ticket code: STU01280712500091920151430Auburn vs LSU
Event: Auburn vs LSU Date: 09/19/2015 Time: 14:30
Section: 1 Row: 28 Seat: 7
Price: $125.00 Ticket Type: STU Cost: $125.00
Raffle Number: 939894
I need to make the cost output have discount included. For example, STU type = 80% discount, and F/S type = 20% and REG = no discount.
Goal is to make the cost include the 80% discount for STU ticket type which would make the cost 25.00 for student and 100.00 for faculty with a 20% discount. I think my problem is concerned with constants but I'm not sure since I'm a beginner.
You need to do an "if" condition with the type (LSU, STU, REG)
price1 = Double.parseDouble(price);
if(type.equals("STU")){
price1 = price1 * STUDENT_DISCOUNT;
}
else if(type.equals("LSU")){
price1 = price1 * FACULTY_STAFF_DISCOUNT;
}

No errors but part of code that should display type and amount of pizzas ordered is not working

public class PizzaExamfinalOne{
public static void main(String[]args){
double total2 = 0;
String customerName = " ";
String address = " ";
String phoneNum ="0";
int max = 5;
int done = 1;
double totalPizza = 0;
double totalA = 0;
int pizzaChoice =0;
String [] pizzaType = {"Placeholder", "Hawaian" , "Classic Italian" , "Cheese Extreme" , "Veg Delight" , "Pepperoni" , "Tomato Margherita" , "Beef & Onion" , "Super Supreme" , "Meat Lovers" , "Hot 'n Spicy" , "Italiano" , "Italian Veg"};
double [] pizzaPrice = {0, 8.5 , 8.5 , 8.5 , 8.5 , 8.5 , 8.5 , 8.5 , 13.5 , 13.5 , 13.5 , 13.5 , 13.5};
int [] orderDisplay = {0,0,0,0,0,0,0,0,0,0,0,0,0};
boolean pickup = true;
int pickup1 = readInt("If you wish to pickup press 1 or press 0 for delivery");
if (pickup1 ==0){
pickup = false;
}
for (int i = 1; i <pizzaType.length; i++){
System.out.println(i + " " +pizzaType[i] + " $" + pizzaPrice[i]);
}
while (done !=0){
pizzaChoice = readInt ("Choose the type of pizza you wish to order");
double pizzaQuantity = readInt ("How many " + pizzaType[pizzaChoice] + " pizzas do you wish to buy (Maximum is 5)");
totalPizza = totalPizza + pizzaQuantity;
if(totalPizza > max){
System.out.println("You cannot order more than five pizzas");
pizzaChoice = 0;
pizzaQuantity = 0;
}
done= readInt ("Press 0 if you are done or press 1 to make another pizza order");
double total1 = pizzaQuantity*pizzaPrice[pizzaChoice];
total2 = total2 + total1;
}
if(pickup==false){
int deliveryCost = 3;
customerName = readString ("Please enter your full name");
address = readString ("Please enter the Delivery address");
phoneNum = readString ("Please enter the phone number of " + customerName);
totalA = total2 + deliveryCost;
System.out.println("Delivery order for " + customerName);
System.out.println("To " + address + ", Phone number 0" + phoneNum);
}
else{
customerName = readString ("Please enter your full name");
System.out.println("Pickup order for " + customerName);
int deliveryCost = 0;
totalA = total2 + deliveryCost;
}
My problem is here. I need to display the pizzas that the user has ordered along with the amount of pizzas ordered and I'm stuck here. there are no errors in the code it just doesnt display the following
for(int i=1;i<orderDisplay.length;i++){
if(orderDisplay[i]>0){
System.out.println(" " + orderDisplay[i] + " " + pizzaType[i] + " pizzas");
}
}
System.out.println("The total amount of your order is " + formatMoney(totalA));
}
public static int readInt(String prompt){
System.out.println(prompt);
java.util.Scanner keyboard = new java.util.Scanner(System.in);
return keyboard.nextInt();
}
public static String readString (String prompt){
System.out.println (prompt);
java.util.Scanner keyboard= new java.util.Scanner(System.in);
return keyboard.nextLine();
}
public static String formatMoney(double phoneNum){
java.text.NumberFormat fmt = java.text.NumberFormat. getCurrencyInstance();
return(fmt.format(phoneNum));
}
}
You don't add orders to your orderDisplay array.
You should handle it in if(pickup==false){..}
You have set all the array elements of orderDisplay to 0.
int [] orderDisplay = {0,0,0,0,0,0,0,0,0,0,0,0,0};
U never changed the value of orderDispaly elements and checking for value greater than 0 and u have started your loop with value 1. check the below lines.
for(int i=1;i<orderDisplay.length;i++){
if(orderDisplay[i]>0){
System.out.println(" " + orderDisplay[i] + " " + pizzaType[i] + " pizzas");
}
Here is your answer :
public class PizzaExamFinalOne
{
public static void main(String[]args)
{
double total2 = 0;
String customerName = " ";
String address = " ";
String phoneNum ="0";
int max = 5;
int done = 1;
double totalPizza = 0;
double totalA = 0;
int pizzaChoice =0;
String [] pizzaType = {"Placeholder", "Hawaian" , "Classic Italian" , "Cheese Extreme" , "Veg Delight" , "Pepperoni" , "Tomato Margherita" , "Beef & Onion" , "Super Supreme" , "Meat Lovers" , "Hot 'n Spicy" , "Italiano" , "Italian Veg"};
double [] pizzaPrice = {0, 8.5 , 8.5 , 8.5 , 8.5 , 8.5 , 8.5 , 8.5 , 13.5 , 13.5 , 13.5 , 13.5 , 13.5};
int [] orderDisplay = {0,0,0,0,0,0,0,0,0,0,0,0,0};
boolean pickup = true;
int pickup1 = readInt("If you wish to pickup press 1 or press 0 for delivery");
if (pickup1 == 0)
{
pickup = false;
}
while (done != 0)
{
for (int i = 1; i <pizzaType.length; i++)
{
System.out.println(i + " " +pizzaType[i] + " $" + pizzaPrice[i]);
}
pizzaChoice = readInt ("Choose the type of pizza you wish to order");
int pizzaQuantity = readInt ("How many " + pizzaType[pizzaChoice] + " pizzas do you wish to buy (Maximum is 5)");
totalPizza = totalPizza + pizzaQuantity;
if(totalPizza > max)
{
totalPizza = totalPizza - pizzaQuantity;
System.out.println("You cannot order more than five pizzas");
pizzaChoice = 0;
pizzaQuantity = 0;
}
else
{
orderDisplay[pizzaChoice] = pizzaQuantity;
}
done= readInt ("Press 0 if you are done or press 1 to make another pizza order");
double total1 = pizzaQuantity*pizzaPrice[pizzaChoice];
total2 = total2 + total1;
}
if(totalPizza > 0)
{
if(pickup == false)
{
int deliveryCost = 3;
customerName = readString ("Please enter your full name");
address = readString ("Please enter the Delivery address");
phoneNum = readString ("Please enter the phone number of " + customerName);
totalA = total2 + deliveryCost;
System.out.println("Delivery order for " + customerName);
System.out.println("To " + address + ", Phone number 0" + phoneNum);
}
else
{
customerName = readString ("Please enter your full name");
System.out.println("Pickup order for " + customerName);
int deliveryCost = 0;
totalA = total2 + deliveryCost;
}
for(int i=1;i<orderDisplay.length;i++)
{
if(orderDisplay[i]>0)
{
System.out.println(" " + orderDisplay[i] + " " + pizzaType[i] + " pizzas");
}
}
System.out.println("The total amount of your order is " + formatMoney(totalA));
}
else
{
System.out.println("You have not ordered anything.");
}
}
public static int readInt(String prompt)
{
System.out.println(prompt);
java.util.Scanner keyboard = new java.util.Scanner(System.in);
return keyboard.nextInt();
}
public static String readString (String prompt)
{
System.out.println (prompt);
java.util.Scanner keyboard= new java.util.Scanner(System.in);
return keyboard.nextLine();
}
public static String formatMoney(double phoneNum)
{
java.text.NumberFormat fmt = java.text.NumberFormat. getCurrencyInstance();
return(fmt.format(phoneNum));
}
}

Categories