Java method not calculating [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am writing a method which calculates the commission a company gives its vendor for a given product, however it is not executing. Please help. Here are the guidelines that I have to follow.
This function calculates the commission a company gives its vendor for a given product. The commission is calculated as follows (I can safely assume amount is greater than 0):
If it is an annual license, the rates are:
10% if sale is between $0 and $1,000
15% if sale is between $1,001 and $10,000
20% if sale is above $10,000
If it is a forever license (not an annual one), the rates are:
10% if sale is between $0 and $10,000
15% if sale is between $10,001 and $50,000
20% if sale is above $50,000
I attempted to write the above logic into the following code:
public static int commissionRate(boolean isAnnualLicense, int saleAmount) {
if (isAnnualLicense == true){
if (saleAmount <= 1000){
return commissionRate(true,10);
} else if (saleAmount >= 1001 && saleAmount <= 10000) {
return commissionRate(true,15);
} else if (saleAmount > 10001) {
return commissionRate(true,20);
} else {
if (isAnnualLicense == false) {
if (saleAmount >= 10000) {
return commissionRate(false,10);
} else if (saleAmount >= 100001 && saleAmount <= 50000) {
return commissionRate(false, 15);
} else if (saleAmount >= 50001) {
return commissionRate(false,20);
}
}
}
}
return 0;
}
This is what it is supposed to be executing:
public void testCommission(){
Assert.assertEquals("commision for annual sales, 1,000 should be 10", 10, Assignment3.commissionRate(true,1000));
Assert.assertEquals("commision for annual sales, 1,001 should be 15", 15, Assignment3.commissionRate(true,1001));
Assert.assertEquals("commision for annual sales, 10,000 should be 15", 15, Assignment3.commissionRate(true,10000));
Assert.assertEquals("commision for annual sales, 10,001 should be 20", 20, Assignment3.commissionRate(true,10001));
Assert.assertEquals("commision for OneTime sales, 10,000 should be 10", 10, Assignment3.commissionRate(false,10000));
Assert.assertEquals("commision for OneTime sales, 10,001 should be 15", 15, Assignment3.commissionRate(false,10001));
Assert.assertEquals("commision for OneTime sales, 50,000 should be 15", 15, Assignment3.commissionRate(false,50000));
Assert.assertEquals("commision for OneTime sales, 50,001 should be 20", 20, Assignment3.commissionRate(false,50001));
}
#Grade(points=25)
#Test
Any help is appreciated. Thanks in advance.

Your code will never reach the isAnnualLicense == false part, because it is at the wrong nesting level. It should be like this:
if (isAnnualLicense){
if (saleAmount <= 1000){
return 10;
} else if (saleAmount >= 1001 && saleAmount <= 10000) {
return 15;
} else { // saleAmount > 10001
return 20;
}
} else {
if (saleAmount <= 10000) {
return 10;
} else if (saleAmount >= 100001 && saleAmount <= 50000) {
return 15;
} else { // saleAmount >= 50001
return 20;
}
}
You can further simplify this by assigning commision rate to a variable, and making a single return at the bottom of your method:
int rate;
if (isAnnualLicense){
if (saleAmount <= 1000){
rate = 10;
} else if (saleAmount >= 1001 && saleAmount <= 10000) {
rate = 15;
} else { // saleAmount > 10001
rate = 20;
}
} else {
if (saleAmount <= 10000) {
rate = 10;
} else if (saleAmount >= 100001 && saleAmount <= 50000) {
rate = 15;
} else { // saleAmount >= 50001
rate = 20;
}
}
return rate;

Related

Java method for updating money with cents over 99

I am trying to create a method so that if the user enters a number of cents over 99, the updateMoney method will add dollars accordingly and then place the extra change once the cents goes under 100.
public void updateMoney(int cent) {
int addDollars = 0;
int change = 0;
if (cent > 99) {
for(int i = cent; i > 99; i -= 100)
{
addDollars += 1;
cent -= 100;
}
}
this.dollars = dollars + addDollars;
this.cents = cent;
}
public Money(int dol, int cent) {
if (cent < 0 || dol < 0) {
System.out.println("Invalid amount entered");
} else {
if (cent > 99) {
updateMoney(cent);
}
this.dollars = dol;
this.cents = cent;
}
}
This is the code I am currently working with.
I had originally tried a different method that ended up not working so I tried doing something like this instead but my outputs are still off.
In my driver I ran
Money money = new Money(15, 300); and the output was $15.00 when it should end up being $18.99
You should consider storing your dollars and cents in one long value. The following code takes your dollars and cents, combines them, adds the user's inputted cents correctly, and splits them up in dollars and cents again. But why not just keep them together all the time?
long dollarsWithCents = dollars * 100 + cents;
dollarsWithCents += parsedUserInput;
cents = dollarsWithCents % 100;
dollars = dollarsWithCents / 100;

if else java algorithm and psuedocode

We would like to assess a service charge for cashing a check. Th service charge depends on the amount of the check. If the check amount is less than 10$, we will charge 1$. If the amount is greater than 10$ but less than 100$, we will charge 10% of the amount. If the amount is greater than 100$, but less than 1,000$, we will charge 5$ plus 5% of the amount. If the value is over 1,000$, we will charge 40$ plus 1% of the amount. Use a multibranch/nested if-else statement to compute for the service charge.
tried writing source code but failed.
I think it will look like this. I put the amount randomly.
int amount = 500;
double pay = 0;
if(amount > 1000){
pay = 40+(amount*0.01);
} else if(amount > 100 && amount < 1000){
pay = 5+(amount*0.05);
} else if(amount > 10 && amount < 100){
pay = amount*0.1;
} else if(amount < 10){
pay = 1;
}
System.out.println(pay+"$")
public double getCharge(int check_amount) {
if (check_amount < 10) {
return 1;
} else if (check_amount < 100) {
return 0.1 * check_amount;
} else if (check_amount < 1000) {
return 5 + 0.05 * check_amount;
} else if (check_amount > 1000) {
return 40 + 0.01 * check_amount;
}
return 0;
}
If we start with the lowest condition, we can neglect to have two conditions in the if statement.
In the main:
public static void main(String[] args) {
System.out.println(getCharge(9)) //when value is less than 10
System.out.println(getCharge(90)) //when value is less than 100
System.out.println(getCharge(900)) //when value is less than 1000
System.out.println(getCharge(5000)) //when value is greater than 1000
}
Output:
1
9
50
90
The question asks for nested if statements, so this code may fair better:
public double getCharge(int check_amount) {
if (check_amount < 1000){
if (check_amount < 100){
if (check_amount <10){
return 1;
}else {
return 0.1*check_amount;
}
} else {
return 5 + 0.05*check_amount;
}
} else {
return 40 + 0.01*check_amount;
}
}
This has the same function as the previous code i posted but as nested if-else statements as per request.

how to implement the string.format method to change the format of this program?

I am trying to figure out how to use the string.format feature into the return statement. Each line has to be a system out? so that means ill have 3 seperate system out?
/*
0 - 4 miles = $2
5 - 15 miles = $5
16 - 25 miles = $10
26 - 50 miles = $15
More than 50 miles = $20
WRITE A PROGRAM THAT CALCULATES TOTAL COST OF USER'S ITEMS
YOUR OUTPUT SHOULD BE FORMATTED AS SEEN BELOW!!
Product Qty Price Miles Total
---- --- ----- ----- -----
Rice 20 5.0 10.0 105.0
Thank you. Come Again!!!
*/
Above is the format that is wanted, I was able to create a method that calculates and returns the correct values, i just need to format the string variable receipt.
public class Test {
public static void main(String[] args) {
methods Rice = new methods();
methods Beans = new methods();
System.out.println(Rice.getTotal("Rice",20,5.0,10));
System.out.println("\n"+ Beans.getTotal("Cake",200,5.75,102.78));
}
}
public class methods {
int qty;
double price;
double miles;
double total;
int i;
String product;
String receipt;
public String getTotal(String product, int qty, double price, double miles){
// 0 - 4 miles = $2
// 5 - 15 miles = $5
// 16 - 25 miles = $10
// 26 - 50 miles = $15
// More than 50 miles = $20
this.qty = qty;
this.price=price;
this.miles = miles;
this.total = 0;
this.product = product;
for(i = 0; i < miles; i++){
if(i < 5){
total = qty * price + 2;
}
else if(i > 4 && i < 16){
total = qty * price + 5;
}
else if(i > 15 && i < 26){
total = qty * price + 10;
}
else if(i > 25 && i < 51){
total = qty * price + 15;
}
else if(i > 50 ){
total = qty * price + 20;
}
}
//return
}
}
I commented out the return statement where the code should go. I can use print method or string.format method.

How do I make a condition that could combine said plans in the program, and add their prices?

I am new to Java.
How do I make a condition that could possibly combine the said "plans" in the program and add their prices? For now the program is only set for one plan and price. Which can only output one plan, and price.
Like for example if I input:
Enter number of talk minutes: 125
Enter number of text messages sent: 225
Enter number of gigabytes used: 1.3
The outcome should be like
Recommended Cellular Plans: Plan B and Plan E
Total Bill: $134.00
import java.text.DecimalFormat;
import java.util.Scanner;
public class HorizonPhones {
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number of talk minutes: ");
int minutes = scanner.nextInt();
System.out.print("Enter amount of text messages sent: ");
int texts = scanner.nextInt();
System.out.print("Enter amount of gigabytes used: ");
double gigaBytes = scanner.nextDouble();
double planA = 49.00;
double planB = 55.00;
double planC = 61.00;
double planD = 70.00;
double planE = 79.00;
double planF = 87.00;
if (minutes <= 500 && texts == 0 && gigaBytes == 0 ) {
System.out.println("Recommended Cellular Plans: Plan A");
System.out.println("Total Bill: " + planA);
return;
}
if (minutes < 500 && texts > 1 && gigaBytes == 0) {
System.out.println("Recommended Cellular Plans: Plan B");
System.out.println("Total Bill: " + planB);
return;
}
if (minutes > 500 && texts <= 100 && gigaBytes == 0) {
System.out.println("Recommended Cellular Plans: Plan C");
System.out.println("Total Bill: " + planC);
return;
}
if (minutes > 500 && texts >= 100 && gigaBytes == 0) {
System.out.println("Recommended Cellular Plans: Plan D");
System.out.println("Total bill: " + planD);
return;
}
if (minutes > 500 && texts >= 1 && gigaBytes < 2) {
System.out.println("Recommended Cellular Plans: Plan E");
System.out.println("Total bill: " + planE);
return;
}
if (minutes > 500 && texts > 1 && gigaBytes > 2) {
System.out.println("Recommended Cellular Plans: Plan F");
System.out.println("Total bill: " + planF);
return;
}
}
}
Before you if(){} blocks start declare two things one String[] (for storing plan's name) and another int sum=0; variable for storing total sum. And remove return from each if(){} block. Then in each block just do this.
String[] plansName = new String[];
int sum = 0;
if (minutes <= 500 && texts == 0 && gigaBytes == 0 ) {
plansName.push("planA");
sum += planA;
}
...
Similarly do for all if(){} blocks and at the end print plansName and sum values.

I need help understanding the programming challenge called Shipping Charges

The question says The Fast Freight Shipping Company charges the following rates:
Weight of Package Rate per 500 Miles Shipped
2 pounds or less $1.10
Over 2 pounds but not more than 6 pounds $2.20
Over 6 pounds but not more than 10 pounds $3.70
Over 10 pounds $3.80
The shipping charge per 500 miles are not prorated. For example, if a 2-pound package is shipped 550 miles, the charges would be $2.20. Write a program that asks the user to enter the weight of a package and then displays the shipping charges.
My problem is that I keep receiving two different answers everytime I put in a weight and distance. For example when I enter the weight as 2 pounds and the distance as 500 miles I get the answers $0.0 and $3.8 which are both incorrect answers. It looks like some weights that I enter are correct answers and others I enter give me incorrect answers. Heres my program:
//import java utilities for scanner class
import java.util.Scanner;
public class ShippingCharge
{
public static void main (String[] args)
{
//Declare and initialize variable to hold the entered weight.
int weight = 0;
//Declare and initialize variable to hold the entered distance.
double distance = 0.0;
//This variable will hold the calculated rate.
double rate;
//This will decide if the shipping charge will advance up one level.
int distanceMultiplier = (int)distance / 500;
//This will hold the increments of the shipping charge.
int distanceRemainder;
//Create a Scanner object for the input.
Scanner input = new Scanner(System.in);
//Get the weight of the package.
System.out.println("What is the weight of the package (in pounds)?");
weight = input.nextInt();
//Get the shipping distance of the package.
System.out.println("What is the shipping distance (in miles)?");
distance = input.nextDouble();
distanceRemainder = (int)distance % 500;
if (distanceRemainder == 0)
{
if (weight <= 2)
System.out.println("Total Shipping Cost is: $" + (distanceMultiplier * 1.10));
}
else if (weight > 2 && weight <= 6)
{
System.out.println("Total Shipping Cost is: $" + (distanceMultiplier * 2.20));
}
else if (weight > 6 && weight <= 10)
{
System.out.println("Total Shipping Cost is: $" + (distanceMultiplier * 3.70));
}
else
{
System.out.println("Total Shipping Cost is: $" + (distanceMultiplier * 3.80));
}
if (distanceRemainder != 0)
{
if (weight <= 2)
System.out.println("Total Shipping Cost is: $" +(distanceMultiplier + 1) * 1.10);
}
else if (weight > 2 && weight <= 6)
{
System.out.println("Total Shipping Cost is: $" +(distanceMultiplier + 1) * 2.20);
}
else if (weight > 6 && weight <= 10)
{
System.out.println("Total Shipping Cost is: $" +(distanceMultiplier + 1) * 3.70);
}
else
{
System.out.println("Total Shipping Cost is: $" +(distanceMultiplier + 1) * 3.80);
}
//end program
System.exit(0);
}//end main
}//end class
This will work for you
public static void main(String[] args) {
int weight = 0;
double distance = 0.0 , distanceExtra ;
Scanner in = new Scanner(System.in);
System.out.println("Weight ? ");
weight = in.nextInt();
System.out.println("Distance ? ");
distance = in.nextDouble();
distanceExtra = distance / 500;
distanceExtra = Math.ceil(distanceExtra);
if (weight <= 2) {
System.out.printf("charge is :" , (distanceExtra * 1.10));
}
else if (weight > 2 && weight <= 6)
{
System.out.printf("charge is :" , (distanceExtra * 2.20));
}
else if (weight > 6 && weight <= 10)
{
System.out.printf("charge is :" , (distanceExtra * 3.70));
}
else if (weight > 10)
{
System.out.printf("charge is :" , (distanceExtra * 4.80));
}
}
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// write your code here
int weight = 0;
double distance = 0.0 ;
Scanner keyboard =new Scanner(System.in);
System.out.println("Enter the Distance");
distance = keyboard.nextDouble();
System.out.println("Enter the Weight");
weight = keyboard.nextInt();
if (weight <= 2) {
System.out.println("charge is : " + "$"+1.10);
}
else if (weight > 2 && weight <= 6)
{
System.out.println("charge is : " + "$"+2.20);
}
else if (weight > 6 && weight <= 10)
{
System.out.println("charge is : " + "$"+3.70);
}
else if (weight > 10)
{
System.out.println("charge is :" + "$"+4.80);
}
}
}

Categories

Resources