Adding a value to a method using a method - java

I'm new to OOP and I've hit a brick wall. I have been trying and trying to get a program that enters price houses and adds fees and taxes. The code below is very hard to get to work, I keep running into "cannot find symbol errors" or "possible lossy conversion between double and int" etc. Can you please help me figure what is going wrong with the following class and tester code? This is only a part of the code that I am working on at the moment. Feel free to ask me questions on it and if needed I'll try my best to clarify what I'm trying to do. Thank you.
The class code;
// method that sets the number of houses
public void setAmountOfHouses(int amountOfHouses)
{
//System.out.println("Enter the amount of houses for sale: "); // prompt
if (amountOfHouses > 0) // if the number of houses is valid
this.amountOfHouses = amountOfHouses; // assign it to instance variable number of houses
else
System.out.println("Please add at least 1 house to database... ");
}
// method that returns the number of houses
public int getAmountOfHouses()
{
return amountOfHouses;
}
// method that set the house price
public void setHousePrice(int housePrice)
{
if(housePrice > 0)
{
this.housePrice = housePrice;
}
else
{
System.out.println("Enter a valid house price ");
}
}
// method that returns the house price
public double getHousePrice()
{
return housePrice;
}
//method to calculate fees and taxes
public void calculateFees(int housePrice)
{
if(housePrice > 0 && housePrice <= 100000)
{
housePriceWithTax = housePrice + tax0; // no tax
totalAuctioneerFeeAmount = housePriceWithTax * auctioneerFee;
totalHousePrice = housePriceWithTax + totalAuctioneerFeeAmount;
}
else if(housePrice >= 100001 && housePrice <= 200000)
{
housePriceWithTax = (((housePrice - 100000) * stampDuty1) + housePrice) + tax0;
totalAuctioneerFeeAmount = housePriceWithTax * auctioneerFee;
totalHousePrice = housePriceWithTax + totalAuctioneerFeeAmount;
}
else if(housePrice >= 200001 && housePrice <= 300000)
{
housePriceWithTax = (((housePrice - 200000) * stampDuty2) + housePrice) + tax0 + tax1;
totalAuctioneerFeeAmount = housePriceWithTax * auctioneerFee;
totalHousePrice = housePriceWithTax + totalAuctioneerFeeAmount;
}
else if(housePrice >= 300001 && housePrice <= 400000)
{
housePriceWithTax = ((housePrice - 300000) * stampDuty3) + housePrice + tax0 + tax1 + tax2;
totalAuctioneerFeeAmount = housePriceWithTax * auctioneerFee;
totalHousePrice = housePriceWithTax + totalAuctioneerFeeAmount;
}
else if(housePrice >= 400001)
{
housePriceWithTax = (((housePrice - 400000) * stampDuty4) + housePrice) + tax0 + tax1 + tax2 + tax3;
totalAuctioneerFeeAmount = housePriceWithTax * auctioneerFee;
totalHousePrice = housePriceWithTax + totalAuctioneerFeeAmount;
}
else
{
housePriceWithTax = (((housePrice - 400000) * stampDuty4) + housePrice) + tax0 + tax1 + tax2 + tax3 + tax4;
totalAuctioneerFeeAmount = housePriceWithTax * auctioneerFee;
totalHousePrice = housePriceWithTax + totalAuctioneerFeeAmount;
}
System.out.println("Base house price is: " + housePrice);
System.out.println("Cost of house with tax added is: " + housePriceWithTax);
System.out.println("Total auctioneer fee on this house is: " + totalAuctioneerFeeAmount);
System.out.println("Total cost of house with stamp duty and fees added is: " + totalHousePrice);
}
}
The tester code;
// Entering the amount of houses
System.out.println("\nEnter the amount of houses for sale: "); // prompt
int amountOfHouses = input.nextInt(); // obtain from input
client1.setAmountOfHouses(amountOfHouses);
if(client1.getAmountOfHouses() > 0)
System.out.println("Adding " + client1.getAmountOfHouses() + " houses to database..." );
else
System.out.println("Returning...");
// setting the price of each house
int counter = 0;
do
{
System.out.println("\nEnter the price of each house for sale: ");
int housePrice = input.nextInt();
client1.setHousePrice(housePrice);
System.out.println("\nAdding house price of " + client1.getHousePrice() + " to database");
counter++;
}
while(counter < amountOfHouses);
// calculating fees and taxes
counter = 0;
do
{
client1.calculateFees(client1.getHousePrice());
counter++;
}
while(counter < amountOfHouses);
}
}

Related

I need User input for my 2 doubles, but there's an Initialization error, but I don't want a set number for the doubles (Java)

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double hourlyPR ;
double hoursWorked ;
double grossPay;
grossPay = hourlyPR * hoursWorked ;
float Withtax = (float) 0.1;
float bigTax = (float) 0.12;
float biggerTax = (float) 0.15;
float giantTax = (float) 0.2;
double bigNetpay = (grossPay - grossPay * bigTax);
double biggerNetpay = (grossPay - grossPay * biggerTax);
double giantNetpay = (grossPay - grossPay * giantTax);
double netPay = (grossPay - grossPay * Withtax);
System.out.println("Enter The Amount of Hours You've Worked:");
hourlyPR = scan.nextDouble();
System.out.println("Enter The Amount You've Earned:");
hoursWorked= scan.nextDouble();
if (grossPay <= 300.00) {
System.out.println("With $" + hourlyPR + " per hour & " + hoursWorked + " hours worked...");
System.out.println("You make: $" + grossPay + " before taxes, " + "and $" + netPay + " after taxes.");
} else if (grossPay >= 300.01 && grossPay <= 400) {
System.out.println("With $" + hourlyPR + " per hour & " + hoursWorked + " hours worked...");
System.out.println("You make: $" + grossPay + " before taxes, " + "and $" + bigNetpay + " after taxes.");
} else if (grossPay >= 400.1 && grossPay <= 500) {
System.out.println("With $" + hourlyPR + " per hour & " + hoursWorked + " hours worked...");
System.out.println("You make: $" + grossPay + " before taxes, " + "and $" + biggerNetpay + " after taxes.");
} else if (grossPay >= 500.1);
System.out.println("With $" + hourlyPR + " per hour & " + hoursWorked + " hours worked...");
System.out.println("You make: $" + grossPay + " before taxes, " + "and $" + giantNetpay + " after taxes.");
}
}
I need the hoursworked and hourlypay doubles to be user input, not intialized. EVerything seems to be fine but the fact that I cannot get user input for those two doubles.
Here is at least one problem. Move scan calls to the top before you try to use them.
Scanner scan = new Scanner(System.in);
System.out.println("Enter The Amount of Hours You've Worked:");
double hourlyPR = scan.nextDouble();
System.out.println("Enter The Amount You've Earned:");
double hoursWorked= scan.nextDouble();
double grossPay = hourlyPR * hoursWorked ;

How can I get rid of the error that I keep getting?

I can't figure out whats wrong or why it won't run. Ive been here for hours messing around with it and I would really like some help. Its for a school project in my intro to programing class. Thanks a lot
so basically heres whats supposed to happen. you type your bill paid the the amount of tip. after that a message will pop up and tell you the percentage of your tip and give a little message about your tip. I keep getting different error messages every time I try to fix the error, another one comes up
public static void main(String[] args) {
String input1 = JOptionPane.showInputDialog (null,
"Enter the Total Bill");
double bill = Double.parseDouble(input1);
String input2 = JOptionPane.showInputDialog(null,
"How much tip did you leave?");
double tipAmount = Double.parseDouble(input2);
double tipPercent = (tipAmount / bill);
if (tipPercent < 0 || tipPercent > 1) {
System.out.println("I don't think that's that correct. ");
} else if (tipPercent >= 0 || tipPercent <= 0.05 ) {
System.out.println("You left a " + tipPercent * 100 "% tip. "
+ "\nBad sercice or enpty wallet? ");
} else if (tipPercent > 0.05 && tipPercent <= 0.1 ) {
System.out.println("You left a " + tipPercent * 100 "% tip. "
+ "\nI think you could have done better");
} else if (tipPercent > 0.1 && tipPercent <= 0.2 ) {
System.out.println("You left a " + tipPercent * 100 "% tip. "
+ "\nNot too bad.";
} else if (tipPercent > 0.2 && tipPercent <= 0.3 ) {
System.out.println("You left a " + tipPercent * 100 "% tip. "
+ "\nSomeone's a bit generous.";
} else if (tipPercent > 0.3 ) {
System.out.println("You left a " + tipPercent * 100 "% tip. "
+ "\nBIG MONEY!!.";
JOptionPane.showMessageDialog(null, message);
}
}
You had two main issues:
message variable is not defined
Last 3 System.out.println statements were missing closing );
With small refactoring it works fine:
public static void main(String[] args) {
String input1 = JOptionPane.showInputDialog (null,
"Enter the Total Bill");
double bill = Double.parseDouble(input1);
String input2 = JOptionPane.showInputDialog(null,
"How much tip did you leave?");
double tipAmount = Double.parseDouble(input2);
double tipPercent = (tipAmount / bill);
String message = getMessage(tipPercent); // you were missing message variable
System.out.println(message);
JOptionPane.showMessageDialog(null, message);
}
private static String getMessage(double tipPercent) {
if (tipPercent < 0 || tipPercent > 1) {
return "I don't think that's that correct. ";
} else if (tipPercent >= 0 || tipPercent <= 0.05 ) {
return "You left a " + tipPercent * 100 +"% tip. "
+ "\nBad sercice or enpty wallet? ";
} else if (tipPercent > 0.05 && tipPercent <= 0.1 ) {
return "You left a " + tipPercent * 100 +"% tip. "
+ "\nI think you could have done better";
} else if (tipPercent > 0.1 && tipPercent <= 0.2 ) {
return "You left a " + tipPercent * 100 +"% tip. "
+ "\nNot too bad.";
} else if (tipPercent > 0.2 && tipPercent <= 0.3 ) {
return "You left a " + tipPercent * 100 +"% tip. "
+ "\nSomeone's a bit generous.";
} else if (tipPercent > 0.3 ) {
return "You left a " + tipPercent * 100 +"% tip. "
+ "\nBIG MONEY!!.";
}
return "Ooops!!"; // edge case, when no if-else statement matches
}

My event handler keeps reporting an error on my overloaded methods

I'm using Java, and I'm trying to call an overloaded method in an event handler. calculateCost() and calculateCost(int,boolean) are the methods I'm overloading, now.
This is the calculateCost(int,boolean) along with the calculateCost() method below:
public double calculateCost() { //calculates cost of computer &peripherals.
if (printerCB.isSelected()) {
return baseComputer() + printer();
}
if (dellMouseCB.isSelected()) {
return baseComputer() + dellMouse();
}
if (logiKeyboardCB.isSelected()) {
return baseComputer() + logiKeyboard();
}
if (canonScannerCB.isSelected()) {
return baseComputer() + canonScanner();
}
if (razerMouseCB.isSelected()) {
return baseComputer() + razerMouse();
}
if (razerKeyboardCB.isSelected()) {
return baseComputer() + razerKeyboard();
}
return baseComputer();
}
public double claculateCost(int quant, boolean shipp) { //calculates the shipping rate for the computer, &peripherals.
quant = 1;
double total = calculateCost();
if (groundShipRB.isSelected() || airShipRB.isSelected() || fedExRB.isSelected()) {
shipp = true;
if (shipp == true) {
{
if (groundShipRB.isSelected()) {
while (quant >= 100) {
return calculateCost() * 0.05;
}
}
if (airShipRB.isSelected()) {
return calculateCost() * 0.10;
}
if (fedExRB.isSelected()) {
return calculateCost() * 0.20;
}
}
}
}
return 0;
}
This is the Code inside the event handler:
private void costCalcWShipButtonActionPerformed(java.awt.event.ActionEvent evt) {
int quant = 1;
double dsqnt = 0.00;
double cost = calculateCost();
boolean shipp = false;
double rate = calculateCost(quant, shipp);
outputTextPane.setText("");
outputTextPane.setText(outputTextPane.getText() + "Quantity." + "\t\t" + "Price." + "\t\t" + "Shipping rate" + "\t\t" + "Quantity discount(per $100)." + "\n");
outputTextPane.setText(outputTextPane.getText() + quant + "\t\t" + cost * quant + "\t\t" + rate + "\t\t" + discountPercentage(quant, dsqnt) + "\n");
for (quant = 100; quant <= 1000; quant = quant + 100) {
outputTextPane.setText(outputTextPane.getText() + quant + "\t\t" + cost * quant + "\t\t" + rate + "\t\t" + discountPercentage(quant, dsqnt) + "\n");
}
}
The calculateCost() method works when I put it into the event handler for the GUI, but when I try calling the calculateCost(int,boolean) it gives me this error:
error: method calculateCost in class compCostCalc cannot be applied to given types;
int quant=1;double dsqnt=0.00;double cost=calculateCost();boolean shipp=false;double rate=calculateCost(quant,shipp);
required: no arguments
found: int,boolean
reason: actual and formal argument lists differ in length
1 error.
This has confused me for a while, any feedback I can get to get it to work?
There's a typo in your code: claculateCost instead of calculateCost (in the method definition).

How to use methods and call them within the Switch statement

I'm making a tax calculator for class but I can't quite figure out how to separate my code in my individual cases
println("Please enter your filing status: ");
println("Enter 0 for single filers,");
println(" 1 for married filing jointly,");
println(" 2 for married filing separately, or");
println(" 3 for head of household");
double tax = 0;
DecimalFormat df = new DecimalFormat("#.00");
int filingStatus = readInt("Status: ");
switch (filingStatus) {
case 0: double taxableIncomes = readDouble("Please enter your taxable income: ");
if (taxableIncomes <= 8925)
{
tax = (taxableIncomes * TEN);
println("You owe: $" + df.format(tax));
}
if (taxableIncomes > 8925 && taxableIncomes <= 36250)
{
tax = 892.5 + FIFTEEN * (taxableIncomes - 8925);
println("You owe: $" + df.format(tax));
}
if (taxableIncomes > 36250 && taxableIncomes <= 87850)
{
tax = 892.5 + 4098.75 + TWENTYFIVE * (taxableIncomes - 36250);
println("You owe: $" + df.format(tax));
}
if (taxableIncomes > 87850 && taxableIncomes <= 183250)
{
tax = 892.5 + 4098.75 + 12900 + TWENTYEIGHT * (taxableIncomes - 87850);
println("You owe: $" + df.format(tax));
}
if (taxableIncomes > 183250 && taxableIncomes <= 398350)
{
tax = 892.5 + 4098.75 + 12900 + 26712 + THIRTYTHREE * (taxableIncomes - 183250);
println("You owe: $" + df.format(tax));
}
if (taxableIncomes > 398350 && taxableIncomes <= 400000)
{
tax = 892.5 + 4098.75 + 12900 + 26712 + 70983 + THIRTYFIVE * (taxableIncomes - 183250);
println("You owe: $" + df.format(tax));
}
if (taxableIncomes > 400000)
{
tax = 892.5 + 4098.75 + 12900 + 26712 + 70983 + 577.5 + THIRTYNINE * (taxableIncomes - 183250);
println("You owe: $" + df.format(tax));
}
break;
into separate methods then call the methods under case:0. For my calculator i'm supposed to use four separate methods and since I have four separate cases I figured the easing thing to do was sort the code for each case into different methods then call those methods for each case that I have. If I try to use
private double tax()
and copy and paste all of the code from under case 0: it tells me that class,interface, or enum expected.

Unexpected result in if else statement

I'm a beginner, so this result I am getting for one condition of my if else statement is blowing my mind. Everything works properly except for the condition when the QtyCalc variable is >= 100. The Finprice variable is listed as the disc variable and I can't figure out why. Help?
import javax.swing.JOptionPane;
public class SoftwareSales {
public static void main(String[] args) {
final int price = 99;
String Qty;
double QtyCalc, preprice, Finprice, disc;
Qty = JOptionPane.showInputDialog(null, "How many packages will you buy?");
QtyCalc = Double.parseDouble(Qty);
preprice = QtyCalc * price;
if (QtyCalc >= 100) {
disc = (preprice * (0.5));
Finprice = (preprice - disc);
JOptionPane.showMessageDialog(null, "Your discount is: " + disc + ".\n" + "Your final price is: " + Finprice + ". ");
} else if (QtyCalc >= 50 && QtyCalc <= 99) {
disc = (preprice * 0.4);
Finprice = (preprice - disc);
JOptionPane.showMessageDialog(null, "Your discount is: " + disc + ".\n" + "Your final price is: " + Finprice + ". ");
} else if (QtyCalc >= 20 && QtyCalc <= 49) {
disc = (preprice * 0.3);
Finprice = (preprice - disc);
JOptionPane.showMessageDialog(null, "Your discount is: " + disc + ".\n" + "Your final price is: " + Finprice + ". ");
} else if (QtyCalc >= 10 && QtyCalc <= 19) {
disc = (preprice * 0.2);
Finprice = (preprice - disc);
JOptionPane.showMessageDialog(null, "Your discount is: " + disc + ".\n" + "Your final price is: " + Finprice + ". ");
} else if (QtyCalc < 10 && QtyCalc >= 1) {
disc = 0;
Finprice = 0;
JOptionPane.showMessageDialog(null, "Sorry, there is no discount for purchases less than 10." + " Your price is: " + preprice);
} else {
JOptionPane.showMessageDialog(null, "You have entered an invalid number.");
}
disc = 0;
Finprice = 0;
System.exit(0);
}
}
The final price is equal to the discount when the quantity is >= 100 because the discount is 50% ... look at the code that you posted. In particular:
if (QtyCalc >= 100) {
disc = (preprice * (0.5));
Finprice = (preprice - disc);
JOptionPane.showMessageDialog(null, "Your discount is: " + disc + ".\n" + "Your final price is: " + Finprice + ". ");
}
There is no strange behavior, just simple math:
disc = (preprice * (0.5));
Finprice = (preprice - disc);
you should also consider eliminating redundant code:
import javax.swing.JOptionPane;
public class SoftwareSales {
public static void main(String[] args) {
final int PRICE = 99;
String qty;
double qtyCalc, preprice, finprice, disc;
disc = 0;
qtyCalc = 0;
boolean invalid = false;
do{
invalid = false;
qty = JOptionPane.showInputDialog(null, "How many packages will you buy?");
if (qty == null){
return;
}
try{
qtyCalc = Double.parseDouble(qty);
}catch(NumberFormatException e){
invalid = true;
}
}while(invalid);
preprice = qtyCalc * PRICE;
if (qtyCalc >= 100) {
disc = (preprice * (0.5));
} else if (qtyCalc >= 50 && qtyCalc <= 99) {
disc = (preprice * 0.4);
} else if (qtyCalc >= 20 && qtyCalc <= 49) {
disc = (preprice * 0.3);
} else if (qtyCalc >= 10 && qtyCalc <= 19) {
disc = (preprice * 0.2);
} else if (qtyCalc < 10 && qtyCalc >= 1) {
disc = 0;
finprice = 0;
JOptionPane.showMessageDialog(null, "Sorry, there is no discount for purchases less than 10. Your price is: " + preprice);
return;
} else {
JOptionPane.showMessageDialog(null, "You have entered an invalid number.");
return;
}
finprice = (preprice - disc);
JOptionPane.showMessageDialog(null, String.format("Your discount is: %.2f.\nYour final price is: %.2f. ",disc,finprice));
}
}
Also remember the java-convention for variables: always begin with lower-case.
finals alsways IN UPPER-CASE

Categories