AWKWARD ZOMBIE

usually not funny
It is currently Wed Jul 30, 2025 7:40 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 336 posts ]  Go to page Previous  1 ... 19, 20, 21, 22, 23
Author Message
 Post subject: Re: Study Buddies (AKA NOW LET US ALL NOT FAIL CLASSES AND S
PostPosted: Mon Nov 18, 2013 9:45 am 
Offline
User avatar

Joined: Wed Nov 03, 2010 12:10 pm
Posts: 11288
Location: Land of Beer and Sausage
O Notation is going to be the death of me and my IT degree.
Can anyone offer help?

_________________
Image

Image


Top
 Profile  
 
 Post subject: Re: Study Buddies (AKA NOW LET US ALL NOT FAIL CLASSES AND S
PostPosted: Sun Mar 30, 2014 1:51 pm 
Offline
No face
User avatar

Joined: Sun Feb 20, 2011 8:18 pm
Posts: 13531
daisies you Java. Okay, I have a Java program I'm writing for homework and there's just one error left for me to fix, except it's not a technical error so Netbeans thinks there's nothing wrong.

The goal is to input imaginary sales for a salesperson, calculate their commission (if any), then output their commission + fixed salary for a total. The sales also need to be shown on a table in $5,000 increments above the actual, capped at 150% of the actual, that shows the potential totals of those increments. This is where the problem is. The table shows up and so do the tiers of sales, but the totals remain the same no matter what. I'll try showing the code I have to see if anyone can figure out what I'm doing wrong.

Code:
import java.util.*;
       
public class Calculator {
public static void main(String[] args) {
   
    Scanner input = new Scanner(System.in);
        //Scanner class imported to utilize keyboard input.
   
    double fixedSalary = 100_000;
    double annualSales;
    double commissionValue;
    double commissionRate = .05;
    double salesTarget = 120_000;
    double salesBegin = 96_000;
    double totalCompensation;
    double maximumAnnualSales;
    double maximumCompensation;
        //Variable names describe what they represent clearly.
       
    System.out.print("Enter salesperson's total annual sales: $");
    annualSales = input.nextDouble();
        //User inputs the amount of sales the salesperson has made.
   
    while (annualSales < 0 || annualSales > 99999999)
    {
        System.out.println("Invalid input.");
        System.out.println();
        System.out.print("Enter salesperson's total annual sales: $");
        annualSales = input.nextDouble();
        //If the input is invalid the user is notified and prompted
        //to enter valid input.
    }
   
    if (annualSales >= salesBegin && annualSales < salesTarget) {
        commissionValue = (annualSales * commissionRate);
        //If the sales are over 80% of the sales target but don't meet it
        //then the sales incentive executes.
    }
    else if (annualSales >= salesTarget) {
        commissionValue = ((annualSales * commissionRate) * 1.25);
        //If the sales meet or exceed the sales target it adds on
        //the acceleration factor to the output.
    }
    else commissionValue = 0;
        //If no conditions are met then there is no commission.
   
    totalCompensation = fixedSalary + commissionValue;
        //Final total
   
    System.out.println("Salesperson's total annual compensation is: "
        + "$"
        + totalCompensation 
    );
        //The program outputs the total annual compensation of the salesperson.
     
    System.out.println();
        //Blank line to separate from the total.
    System.out.println("Total Possible Sales \t Total Possible Annual Compensation" );
   
    maximumAnnualSales = annualSales * 1.5;
        //Maximum potential sales are 150% of actual.
     
    do {
        System.out.println("$" + annualSales
        + "\t\t" + "$" + totalCompensation);
       
        annualSales = annualSales + 5000;
        }
    while (annualSales <= maximumAnnualSales);
    //Loop takes annual sales and projects to 1.5 times the amount. Intention
    //is for annual compensation to do the same but the table shows identical
    //values and I can't figure out why since there are no explicit errors.
}       
    }

_________________
Stuff goes here later.


Top
 Profile  
 
 Post subject: Re: Study Buddies (AKA NOW LET US ALL NOT FAIL CLASSES AND S
PostPosted: Sun Mar 30, 2014 3:11 pm 
Offline
User avatar

Joined: Wed Nov 03, 2010 12:10 pm
Posts: 11288
Location: Land of Beer and Sausage
I've never seen a while loop where the while is at the end. Have you tried using a repeat-until loop instead?
Unrelated, but you should idiot proof your input prompt. I think your program would throw a type mismatch exception if the user input something that's not a double.

Wait, I think I got it. Your compensation value simply never updates in the loop, so it's always the same value. Yeah I think that's it. You update your annual sales but your program doesn't go through the steps to calculate the comission anymore at that point.

_________________
Image

Image


Top
 Profile  
 
 Post subject: Re: Study Buddies (AKA NOW LET US ALL NOT FAIL CLASSES AND S
PostPosted: Sun Mar 30, 2014 3:36 pm 
Offline
No face
User avatar

Joined: Sun Feb 20, 2011 8:18 pm
Posts: 13531
I don't think that's it. I tried copy-pasting the sections where it does the calculations and while there are no errors it still outputs the same total.

Also I'm not sure how to idiot-proof the prompt.

EDIT: Wait I'm stupid. I wasn't supposed to copy-paste the calculations, I was supposed to copy-paste the conditionals that tell the program which calculation to use. Everything works perfectly now.

_________________
Stuff goes here later.


Top
 Profile  
 
 Post subject: Re: Study Buddies (AKA NOW LET US ALL NOT FAIL CLASSES AND S
PostPosted: Sun Mar 30, 2014 4:04 pm 
Offline
User avatar

Joined: Wed Nov 03, 2010 12:10 pm
Posts: 11288
Location: Land of Beer and Sausage
Idiot proofing would be using a try - catch here.

try {
//what's supposed to be done } catch ( //exceptionname with some variable name, in this case it would be typeMismatchException I think but you can just use the generic Exception e) { //what to do if that exception happens, for example setting annualSales to -1 so it goes into that condition that lets you reinput}

Code:
try {
   annualSales = input.nextDouble(); }
         catch (Exception e) {annualSales = -1; }

_________________
Image

Image


Top
 Profile  
 
 Post subject: Re: Study Buddies (AKA NOW LET US ALL NOT FAIL CLASSES AND S
PostPosted: Sun Mar 30, 2014 4:16 pm 
Offline
No face
User avatar

Joined: Sun Feb 20, 2011 8:18 pm
Posts: 13531
That's a handy bit of code, thanks.

_________________
Stuff goes here later.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 336 posts ]  Go to page Previous  1 ... 19, 20, 21, 22, 23

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 33 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group