I need help figuring out what is wrong with my code - java

I'm getting some errors saying that it cannot find symbol
import javax.swing.JOptionPane;
public class ShippingSales
{
public static void main (String [] args)
{
int weight, miles;
String temp;
double shippingcharge, rate;
shippingcharge = miles * rate;
miles = 500;
temp = JOptionPane.showInputDialog("Enter the weight of the package");
weight = Interger.parseInt(temp);
if (weight <= 2)
{
JOptionPane.showMessageDialog(null, "Your total amount is " + shipppingcharge);
rate = 1.10;
}
if (weight > 2 && weight<= 6 )
{
JOptionPane.showMessageDialog(null, "Your total amount is " + shipppingcharge);
rate = 2.20;
}
if (weight > 6 && weight<= 10)
{
JOptionPane.showMessageDialog(null, "Your total amount is " + shipppingcharge);
rate = 3.70;
}
if (weight > 10)
{
JOptionPane.showMessageDialog(null, "Your total amount is " + shipppingcharge);
rate = 3.80;
}
}
}

Interger.parseInt(temp);
Anything wrong with this line you can see at second glance?
Hint: Spelling.
Also, you misspelled "shipping charge" in a variable reference multiple times.

Well you used three "p"s in all of the showMessageDialogs but the variable is called shippingcharge
And you spelled Integer wrong, you used Interger

Related

Why is it that my class is showing as not being used and thus can't compile my program?

extremely new to java and in my class(in school no the program) we are to design a class that stores weight and a method that returns the shipping charges. I'm clearly very confused as to what I'm doing here. I have this code but can't even compile as it reads that my class isn't used. Any help would be great.
import javax.swing.JOptionPane;
public class ShippingCharges {
public float weight;
public void myPublicMethod() {
weight = Float.parseFloat(JOptionPane.showInputDialog(null, "What is the weight in kg?: ", JOptionPane.QUESTION_MESSAGE));
}
public static double main(String[] args) {
ShippingCharges charge = new ShippingCharges();
charge.myPublicMethod();
if (charge.weight >= 2) {
System.out.println("The shipping charge for 2kg or less for every 500 miles is $1.10");
return 1.10;
}
if (charge.weight > 2 && charge.weight <= 6) {
System.out.println("The shipping charge for over 2kg up to 6kg " +
"for every 500 miles is $2.20");
return 2.20;
}
if (charge.weight > 6 && charge.weight <= 10) {
System.out.println("The shipping charge for over 6kg up to 10kg " +
"for every 500 miles is $2.20");
return 3.70;
} else {
System.out.println("The shipping charge for anything over 10kg" +
"for every 500 miles is $4.80");
return 4.80;
}
}
}
also sorry if this has been asked before. I can't seem to find it
main does not return a double. The double return should be on your method. The return type for main is void.
Try this: I took the liberty of changing float to double. You can change them back if you want.
public class ShippingCharges {
public double weight;
public static double myPublicMethod() {
ShippingCharges charge = new ShippingCharges();
double weight = Double.parseDouble(JOptionPane.showInputDialog(null, "What is the weight in kg?: ", JOptionPane.QUESTION_MESSAGE));
if (charge.weight >= 2) {
System.out.println("The shipping charge for 2kg or less for every 500 miles is $1.10");
return 1.10;
}
if (charge.weight > 2 && charge.weight <= 6) {
System.out.println("The shipping charge for over 2kg up to 6kg " +
"for every 500 miles is $2.20");
return 2.20;
}
if (charge.weight > 6 && charge.weight <= 10) {
System.out.println("The shipping charge for over 6kg up to 10kg " +
"for every 500 miles is $2.20");
return 3.70;
} else {
System.out.println("The shipping charge for anything over 10kg" +
"for every 500 miles is $4.80");
return 4.80;
}
}
public static void main(String[] args) {
myPublicMethod();
}
}
public class ShippingCharges {
private float weight;
public void myPublicMethod() {
weight = Float.parseFloat(JOptionPane.showInputDialog(null, "What is the weight in kg?: ", JOptionPane.QUESTION_MESSAGE));
}
private double computeShippingCharges() {
if (weight >= 2) {
System.out.println("The shipping charge for 2kg or less for every 500 miles is $1.10");
return 1.10;
}
if (weight > 2 && weight <= 6) {
System.out.println("The shipping charge for over 2kg up to 6kg for every 500 miles is $2.20");
return 2.20;
}
if (weight > 6 && weight <= 10) {
System.out.println("The shipping charge for over 6kg up to 10kg for every 500 miles is $2.20");
return 3.70;
}
System.out.println("The shipping charge for anything over 10kg for every 500 miles is $4.80");
return 4.80;
}
public static void main(String [] args) {
ShippingCharges charge = new ShippingCharges();
charge.myPublicMethod();
charge.computeShippingCharges();
}
}

Aggregating results of different calculations

This is my first post here, so forgive me for any formatting errors.
So as you can see my program requests the gender, # of accidents and year of car to display a fictitious insurance quote.
Based on all that information I need to add the subtotal of the insurance cost to the end.
I have my code working up until the Total Cost comment (posted it all for reference). I am stuck there because the genders have different base amounts. I'm trying to figure out a way to only do one if statement if it matches the gender that was input by the user.
Any ideas?
import java.util.*;
public class Insurance {
public static void main(String [] args) {
Scanner scanner = new Scanner(System.in);
int currentYear = 2017; //used for calculating the age of the users car
int maleGender = 1000;
int femaleGender = 500;
//Letting user know they are inputting data for car insurance purposes
System.out.println("Car insurance questionnaire. Please input correct information when prompted.");
// gender information from user
System.out.println("What is your gender? m/f");
String gender = scanner.next();
// accident quantity information from user
System.out.println("How many accidents have you had?");
int acc = scanner.nextInt();
// car year information from user
System.out.println("What year was your car manufactured?");
int carAge = scanner.nextInt();
//if statements which refer to the users data input
if (gender.equals("m")) {
System.out.println("You are a male.\nThe base cost is $1000.");
} else {
System.out.println("You are a female.\nThe base cost is $500.");
}
if (acc == 0) {
System.out.println("You have no accidents. Insurance increase is $0.");
} else if (acc >= 1) {
System.out.println("You have " + acc + " accidents. Insurance increase is $" + acc * 100 + ".");
}
if (carAge >= 2007) {
System.out.println("Your car is " + (currentYear - carAge) + " years old.\nYour car is still in warranty, no savings added.");
} else
System.out.println("Your car is out of warranty, final cost is halved.");
//Total cost
/*
if (carAge <= 2007) {
System.out.println("Your total price is $" + ((acc * 100 + femaleGender) / 2) + ".");
} else
System.out.println("Your total price is $" + (acc * 100 + femaleGender) + ".");
*/
}
}
I am not totally sure how you want to calculate your result but if do NOT want to use femaleGender all the time but in dependency of the gender different values then maybe something like this could help:
int baseAmount = gender.equals("m") ? maleGender : femaleGender;
if (carAge <= 2007) {
System.out.println("Your total price is $" + ((acc * 100 + baseAmount ) / 2) + ".");
} else
System.out.println("Your total price is $" + (acc * 100 + baseAmount ) + ".");
}
int genderCost;
...
if (gender.equals("m")) {
System.out.println("You are a male.\nThe base cost is $1000.");
genderCost = maleGender;
} else {
System.out.println("You are a female.\nThe base cost is $500.");
genderCost = femaleGender;
}
...
if (carAge <= 2007) {
System.out.println("Your total price is $" + ((acc * 100 + genderCost) / 2) + ".");
} else
System.out.println("Your total price is $" + (acc * 100 + genderCost) + ".");
}
Put the amount for gender in a variable genderCost when the gender input variable is evaluated and use genderCost when you calculate the total.

Account class error 2

I am still having trouble figuring out how the heck the most efficient way to do this is.. Basically, I am trying to make the balance = 0 for every object in the Account array created. I tried using a for loop and set balance = 0for each account created, but I am unsure of how to make this work since the balance variable is created in the class that has all of the methods. I have been trying to this problem all day, but no luck. Thanks.
Main method:
import java.util.Scanner;
import java.text.NumberFormat;
public class Account2
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
Account[] acct = new Account[30];
for (int count2; count2 < 30; count2++)
{
balance = 0; //Initial balance is always set to zero to be able to run fresh program every time
}
System.out.println("Enter your account number (1-30): ");
int key = scan.nextInt() - 1;
int reset = 0;
while (reset == 0)
{
System.out.println("Enter W for withdrawl; D for deposit; X to escape");
char choice = scan.next().charAt(0);
if (choice == 'W' || choice == 'w' || choice == 'D' || choice == 'd' || choice == 'x' || choice == 'X')
{
if (choice == 'W' || choice == 'w')
{
System.out.println("Enter amount to withdraw: ");
Double withdraw1 = scan.nextDouble();
if (withdraw1 <= acct[key].getBalance())
{
acct[key].withdraw(withdraw1);
System.out.println("User # " + key++ + " funds after withdraw: " + acct[key].getBalance() + "$");
System.out.println("User # " + key++ + " funds after interest: " + acct[key].addInterest() + "$");
reset++;
}
else
System.out.println("Insufficient funds.");
}
if (choice == 'D' || choice == 'd')
{
System.out.println("Enter amount to deposit: ");
Double deposit1 = scan.nextDouble();
if (deposit1 > 0)
{
acct[key].deposit(deposit1);
System.out.println("User # " + key++ + " funds after deposit: " + acct[key].getBalance() + "$");
System.out.println("User # " + key++ + " funds after interest: " + acct[key].addInterest() + "$");
reset++;
}
else
System.out.println("Use the withdrawl feature to withdrawl money.");
}
if (choice == 'x' || choice == 'X')
System.out.println("Thank You for using this bank.");
reset++;
}
else
{
System.out.println("Invalid entry, please try again");
reset = 0;
}
}
}
}
Supporting class:
public class Account
{
private final double RATE = 0.03; //Interest is 3%
private int acctNumber;
private String name;
private balance;
//Defines owner, account number, and initial balance.
public Account(String owner, int account, double initial)
{
name = owner;
acctNumber = account;
balance = initial;
}
//deposits a specified amount and returns new balance
public double deposit(double amount)
{
balance = balance + amount;
return balance;
}
//withdraws the specified amount from the account and applies the fee
// + returns balance
public double withdraw(double amount)
{
int fee = 1;
balance = balance - amount - fee;
return balance;
}
//Adds interest to the account
public double addInterest()
{
balance += (balance * RATE);
return balance;
}
public double getBalance()
{
return balance;
}
//returns a one line description of the account as a string
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
return acctNumber + "/t" + name + "/t" + fmt.format(balance);
}
}
From an outside class, you can only interact with Account in the way exposed in its visible (e.g. public) API.
In this case, the current way to do this would be to withdraw() the current balance:
acct[i].withdraw(acct[i].getBalance());
Though this specific case would put the balance into the negatives because you charge a fee for a withdrawal (which is hidden from the calling class).
If you were to expose a setBalance method on Account, you could instead do
acct[i].setBalance(0);
However on closer look, it seems like what you are having trouble with is actually initializing the array of accounts. You can do this like this:
for (int count2; count2 < 30; count2++)
{
acct[count2] = new Account(owner, count2, 0);
}

Printing out a while loop

import java.util.Scanner;
public class Teacher{
public static void main(String [] args){
Scanner reader = new Scanner(System.in);
double salary;
double pi;
int year;
int years = 1;
double predict;
double predict2 = 0;
double sum = 0;
System.out.print("What is your starting salary: ");
salary = reader.nextDouble();
System.out.print("What is your precentage increase: ");
pi = reader.nextDouble();
System.out.print("How many years are you working: ");
year = reader.nextInt();
if (salary <= 0){
System.out.print("The salary must be positive.");
}
if (pi <= 0){
System.out.print("The percentage increase must be positive.");
}
if (year < 0){
System.out.print("The years must be positive.");
}
while (year > years) {
predict = salary * (pi/100);
System.out.println(years + ". " + predict);
years++;
if (years == year){
break;
}
}
}
}
I am having trouble trying to print out a loop. Every time I run the program, this segment only prints out one number and doesn't print out the rest.
per #ajb wouldn't you want to do something like this in your loop? This will print your salary increase every year, and also add that to the salary for the next iteration
Per your new comment
Every time their is an output, I would like it to display the year and
their salary.
while (year > years) {
//predict = salary * (pi/100); commented this because it is not necessary anymore.
salary += salary * (pi/100);
System.out.println(years + ". " + salary); // replaced predict with salary, to show their salary and not just their predicted raise.
years++;
// This block of code will never be hit, therefore it is not needed.
//if (years == year){
// break;
//}
}
Revised could be something like this.
while (year > years) {
salary += salary * (pi/100);
System.out.println(years + ". " + salary);
years++;
}

data validation

I am writing a loan calculate with data validation. My maximum loan amount is 1,000,000 and I am using the method below to validate. When I enter 1,000,000 into the program it comes back with my error method. I thought (d >= max) would allow me to go up to and including my max. Can anyone see a problem with this method or is it possible I should be looking elsewhere in my code for a problem.
Any help is appreciated.
public static double getDoubleWithinRange(Scanner sc, String prompt, double min, double max)
{
double d = 0.0;
boolean isValid = false;
while (isValid == false)
{
d = getDouble (sc, prompt);
if (d <= min)
{
System.out.println(
"Error! Number must be greater than " + min + "." );
}
else if (d >= max)
{
System.out.println("Error! Number must be less than " + max + "." );
}
else
isValid = true;
}
return d;
//Get input from user
System.out.println("DATA ENTRY");
double loanAmount = getDoubleWithinRange (sc,
"Enter loan amount: ",0, 1000000);
double interestRate = getDoubleWithinRange (sc,
"Enter yearly interest rate: " , 0, 20);
int years = getIntWithinRange (sc,
"Enter number of years: ",0,100);
you are saying if the amount is greater than or equal to one million cause an error. you want to say if it is greater than show an error
Use else if (d>max), since you want up to 1,000,000 (and 1,000,000 can be included).

Categories