Creating a simple tuition program and I'm getting stuck on (what I think are) the finishing touches.
The issue is I'm not sure how to compute the "hours" variable to remain constant throughout everything once it's been stated.
This is my first year with Java so please excuse my bad style.
import java.util.*;
import javax.swing.JOptionPane;
/**
The application calculates the cost of tuition for one semester at OCC.
*/
public class TuitionOCC
{
public static void main(String[] args)
{
String input;
int residency;
byte credits;
int residentTuition;
int nonTuition;
int internationalTuition;
int hours;
double tuition;
residentTuition = 82;
nonTuition = 154;
internationalTuition = 216;
JOptionPane.showMessageDialog(null, "Calculate your tuition!", "Tuition",JOptionPane.INFORMATION_MESSAGE);
input = JOptionPane.showInputDialog("Are you a:\n" +
"1- College District Resident\n" +
"2- Non-Resident of College District\n" +
"3- Out-of-State or International Student");
residency = Integer.parseInt(input);
if (residency == 1)
{
input = JOptionPane.showInputDialog("How many credit hours are you taking?: ");
hours = Integer.parseInt(input);
if (hours <=0)
JOptionPane.showMessageDialog(null,"You must enter a value of 1 or more.\n" + "Please run the program again", "Error", JOptionPane.ERROR_MESSAGE);
}
else if (residency == 2)
{
residency = nonTuition;
input = JOptionPane.showInputDialog("How many credit hours are you taking?: ");
hours = Integer.parseInt(input);
if (hours <=0)
JOptionPane.showMessageDialog(null,"You must enter a value of 1 or more.\n" + "Please run the program again", "Error", JOptionPane.ERROR_MESSAGE);
}
else if (residency == 3)
{
residency = internationalTuition;
input = JOptionPane.showInputDialog("How many credit hours are you taking?: ");
hours = Integer.parseInt(input);
if (hours <=0)
JOptionPane.showMessageDialog(null,"You must enter a value of 1 or more.\n" + "Please run the program again", "Error", JOptionPane.ERROR_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null, "You must enter a 1, 2, or 3\n" + "Please run the program again", "Error", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
tuition = hours * residency;
if
(residency == 1)
{
JOptionPane.showMessageDialog(null, hours + " hours at $ " + residentTuition + "per hour yields a tuition of " + tuition);
}
else if
(residency == 2)
{
JOptionPane.showMessageDialog(null, hours + " hours at $ " + nonTuition + "per hour yields a tuition of " + tuition);
}
else if
(residency == 3)
{
JOptionPane.showMessageDialog(null, hours + " hours at $ " + internationalTuition + "per hour yields a tuition of " + tuition);
}
}
}
If you want to make the hours constant, then you can do something like this:
input = JOptionPane.showInputDialog("How many credit hours are you taking?: ");
final hours = Integer.parseInt(input);
You can also put the JOptionPane on top of if statements so that you will not reassign values on each block of if statements.
Similarly, you can do this to your code to shorten everything.
import java.util.*;
import javax.swing.JOptionPane;
/**
The application calculates the cost of tuition for one semester at OCC.
*/
public class TuitionOCC {
public static void main (String[] args) {
String input;
int residency;
byte credits;
int residentTuition;
int nonTuition;
int internationalTuition;
double tuition;
residentTuition = 82;
nonTuition = 154;
internationalTuition = 216;
JOptionPane.showMessageDialog(null, "Calculate your tuition!", "Tuition",JOptionPane.INFORMATION_MESSAGE);
input = JOptionPane.showInputDialog("Are you a:\n" +
"1- College District Resident\n" +
"2- Non-Resident of College District\n" +
"3- Out-of-State or International Student");
residency = Integer.parseInt(input);
input = JOptionPane.showInputDialog("How many credit hours are you taking?: ");
final int hours = Integer.parseInt(input);
if (hours <= 0) {
JOptionPane.showMessageDialog(null,"You must enter a value of 1 or more.\n" + "Please run the program again", "Error", JOptionPane.ERROR_MESSAGE);
}
else {
tuition = hours * residency;
if (residency == 1) {
JOptionPane.showMessageDialog(null, hours + " hours at $ " + residentTuition + "per hour yields a tuition of " + tuition);
} else if (residency == 2) {
JOptionPane.showMessageDialog(null, hours + " hours at $ " + nonTuition + "per hour yields a tuition of " + tuition);
} else if (residency == 3) {
JOptionPane.showMessageDialog(null, hours + " hours at $ " + internationalTuition + "per hour yields a tuition of " + tuition);
} else {
JOptionPane.showMessageDialog(null, "You must enter a 1, 2, or 3\n" + "Please run the program again", "Error", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
}
}
Related
I've been working on a mortgage calculator for my C++ class. However, I am stuck.
I got the following formula off of Nerdwallet and tried to implement it in my program:
M = P [ i(1 + i)^n ] / [ (1 + i)^n – 1]
M=mortgage payment
P=Principal
i=interest
n=number of payments
Here's the code that I am currently using.
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
int main(int argc, char** argv)
{
int years, homePrice, creditScore, totalPayments;
float rate, mortgageTotal, monthlyPayment;
cout << "What is the price of the home that you are looking to mortgage?\n";
cin >> homePrice; //Assigns the variable homePrice to a value
cout << "What is your credit score?\n";
cin >> creditScore; //Assigns the creditScore variable a value
cout << "Would you prefer a 15 year or 30 year mortgage? Please type 15 or 30.\n";
cin >> years;
if ( years = 15 ) //If input is 15 year it will go down this logical path
{
if (creditScore >=760) //If their credit rating is equal to or more than 760, their rate will be .043 also nested if.
{
rate = .043;
cout << "Your interest rate is 4.3%\n";
}
else if (creditScore >= 700) //If their credit rating is equal to or more than 700, their rate will be .0455
{
rate = .0455;
cout << "Your interest rate is 4.55%\n";
}
else if (creditScore >= 660) //If their credit rating is equal to or more than 660, their rate will be .048
{
rate = .048;
cout << "Your interest rate is 4.8%\n";
}
else if (creditScore >= 620) //If their credit rating is equal to or more than 620, their rate will be .058
{
rate = .058;
cout << "Your interest rate is 5.8%\n";
}
else if (creditScore >= 580) //If their credit rating is equal to or more than 580, their rate will be .0655
{
rate = .0655;
cout << "Your interest rate is 6.55%\n";
}
else if (creditScore >= 500) //If their credit rating is equal to or more than 500, their rate will be .083
{
rate = .083;
cout << "Your interest rate is 8.3%\n";
}
}
else if ( years=30 )
{
if (creditScore >= 760)
{
rate=.043;
cout <<"Your interest rate is 4.3%\n";
}
else if (creditScore >= 700)
{
rate=.0455;
cout << "Your interest rate is 4.55%\n";
}
else if (creditScore >= 660)
{
rate=.048;
cout << "Your interest rate is 4.8%\n";
}
else if (creditScore >= 620)
{
rate=.058;
cout << "Your interest rate is 5.8%\n";
}
else if (creditScore >= 580)
{
rate=.0655;
cout << "Your interest rate is 6.55%\n";
}
else if (creditScore >= 500)
{
rate=.083;
cout << "Your interest rate is 8.3%\n";
}
}
totalPayments = years * 12;
monthlyPayment = homePrice * [[rate * (1 + rate)pow(totalPayments)] / [(1 + rate)pow(totalPayments) - 1]];
mortgageTotal = monthlyPayment * totalPayments;
cout << "Your mortgage will cost approximately " << mortgageTotal << " and your monthly payment will be " << monthlyPayment << endl;
return 0;
}
However, when I go to compile it, I get the following errors:
Errors
I just don't understand the errors and why they are there.
If someone could help me, I'd greatly appreciate it.
Thank you.
While your math formula uses both []s and ()s for grouping expressions, only ()s can be used in such a way in C++.
pow is a function call, not an in-place operator like you seem to be using it. It needs to look like pow(1 + rate, totalPayments).
Your ifs also are doing assignments (=) instead of comparisons (==). As it is, your code will only follow the first if because it is setting years to 15.
Your mortgage formula seems wrong. Try this
monthlyPayment = homePrice * ((rate * pow(1 + rate, totalPayments)) /
(pow(1.00 + rate, totalPayments) - 1));
First time posting here. Recently started a C++ class and I'm really enjoying it! I'm creating a tax computing program but ran into a bit of wall on this part. I don't know how its not initialized if I already stated it using 'double'
The variable that I'm having trouble with is "taxes2" on line 129
Please help if you can!
Also, this is the prompt im working with:
"Write a program to compute income tax as follows:
First, read the income for the year. Then, display the following menu and ask the user to select his or her filing status:
Single
Married filing separately
Married filing jointly
Head of household
For Single status, apply a deduction of $2000, for #2 $1700, for #3, $2300 and Head of household $2700. Also, ask for and read the number of dependents. Add to the deduction amount, $150 for each dependent up to a maximum of 3 dependents if single, add $125 for each dependent up to a maximum of 4 for Married filing separately, and $175 for each dependent, up to a maximum of 5, if married filing jointly or head of household.
Then, compute the taxable income by subtracting the total deduction computed above from his or her income. Anyone with a taxable income of less than $8,000 pays no taxes. For the rest, the first $15,000 of the income has a tax rate of 10%, the next $25,000 gets taxed at 15%, the next $30,000 at 20%, the next $30,000 at 25% and anything above $100,000 at 30%. So, for example, someone who had taxable income of $125,000, her first $15,000 gets taxed at 10% ($1,500), plus 15% of $25,000 ($3,750), plus 20% of $30,000 ($6,000), plus 25% of $30,000 ($7,500) and the remaining $25,000 at 30% ($7,500) for a total tax amount of $26,250. For someone with income of $13,000, the tax is 10% of that or $1,300. For someone who made $7,999.99 it would be 0, but for someone who made $8,000, it's $800. For someone who made $30,000, it would be 10% of $15,000 or $1,500 plus 15% of the remaining $15,000 or $2,250 making it $3,750, and so on. Display the amount of taxes to be paid."
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
const double tax1 = 1500.00;
const double tax2 = 3750.00;
const double tax3 = 6000.00;
const double tax4 = 7500.00;
double income;
double istatus;
double itaxes;
double itaxes2;
double ideduction1;
double ideduction2;
double idependant;
const double idependantrate1 = 150;
const double idependantrate2 = 125;
const double idependantrate3 = 175;
double idependanttotal;
cout << "What is your income? " << endl;
cin >> income;
cout << "Please enter your filing status: \n";
cout << "1. Single \n";
cout << "2. Married Filing Separately \n";
cout << "3. Married Filing Jointly \n";
cout << "4. Head of Household \n";
cin >> istatus;
// Begin Deductions
if (istatus == 1)
{
cout << "You've selected Single";
ideduction1 = 2000;
cout << "Please select number of dependants (max: 3): ";
cin >> idependant;
if (idependant >= 0)
{
idependant * idependantrate1 == idependanttotal;
}
idependanttotal + ideduction1 == ideduction2;
}
else if (istatus == 2)
{
cout << "You've selected Married Filing Separately";
ideduction1 = 1700;
cout << "Please select number of dependants (max: 4): ";
cin >> idependant;
if (idependant >= 0)
{
idependant * idependantrate2 == idependanttotal;
}
idependanttotal + ideduction1 == ideduction2;
}
else if (istatus == 3)
{
cout << "You've selected Married Jointly";
ideduction1 = 2300;
cout << "Please select number of dependants (max: 5): ";
cin >> idependant;
if (idependant >= 0)
{
idependant * idependantrate3 == idependanttotal;
}
idependanttotal + ideduction1 == ideduction2;
}
else if (istatus == 4)
{
cout << "You've selected Head of Household";
ideduction1 = 2700;
cout << "Please select number of dependants (max: 5): ";
cin >> idependant;
if (idependant >= 0)
{
idependant * idependantrate3 == idependanttotal;
}
idependanttotal + ideduction1 == ideduction2;
}
// Calculate Income Taxes
if (income <= 7999.99)
{
cout << "No taxes need to be paid. " << endl;
return 0;
}
else if (income >= 8000.00 && income <= 15000.00)
{
itaxes = income * .10; //calculates tax amount
itaxes - ideduction2 == itaxes2;
cout << "The amount owed in taxes is: $" << itaxes2 << endl;
}
else if (income >= 15000.01 && income <= 40000.00)
{
itaxes = ((income - 15000) * .15) + tax1; //calculates tax amount
itaxes - ideduction2 == itaxes2;
cout << "The amount owed in taxes is: $" << itaxes2 << endl;
}
else if (income >= 40000.01 && income <= 70000.00)
{
itaxes = ((income - 40000) * .20) + tax1 + tax2; //calculates tax amount
itaxes - ideduction2 == itaxes2;
cout << "The amount owed in taxes is: $" << itaxes2 << endl;
}
else if (income >= 70000.01 && income <= 100000.00)
{
itaxes = ((income - 70000) * .25) + tax1 + tax2 + tax3; //calculates tax amount
itaxes - ideduction2 == itaxes2;
cout << "The amount owed in taxes is: $" << itaxes2 << endl;
}
else if (income > 100000.01)
{
itaxes = ((income - 100000) * .30) + tax1 + tax2 + tax3 + tax4; //calculates tax amount
itaxes - ideduction2 == itaxes2;
cout << "The amount owed in taxes is: $" << itaxes2 << endl;
}
else
cout << "You have entered an unuseable number. Please restart the program and enter a positive number " << endl;
return 0;
idependant * idependantrate1 == idependanttotal will calculate the value of idependant * idependantrate1, and then check if it equals idependanttotal. If you want to store the calculated value in idependanttotal write idependanttotal = idependant * idependantrate1;.
This is a program I am doing for programming class that that is supposed calculate people's pay depending on overtime and time and a half, but it broke and I have no idea why.
The thing will output "" "" has job 6942646 and worked 6923592 hours" or something like that.
#include <iostream>
#include <iomanip>
using namespace std;
string getName(string, string);
int getJob(int&);
int hoursWorked(int);
double jobCalc(int&, int&, double&);
void display(string, string, int, int, double);
int main()
{
int job;
int hours;
double pay;
string firstname, lastname;
getName(firstname, lastname);
getJob(job);
hoursWorked(hours);
jobCalc(job, hours, pay);
display(firstname, lastname, job, hours, pay);
return 0;
}
string getName(string firstname, string lastname)
{
cout << "Enter your name (First): ";
cin >> firstname;
cout << "Enter your name (Last): ";
cin >> lastname;
return firstname, lastname;
}
int getJob(int job&)
{
cout << "Yo What motha duckin job number is you? Ya' dig? (10, 20, or 30): ";
cin >> job;
bool jobNo = false;
while (jobNo = false)
{
if (job == 10)
{
jobNo = true;
}
else if (job == 20)
{
jobNo = true;
}
else if (job == 30)
{
jobNo = true;
}
else if (job < 9 || job > 11 || job < 19 || job > 21 || job < 29 || job > 31)
{
cout << "Yo! Please re-enter a correct value. What it is, mostly, Mama! Don't make me shank ya!" << endl;
cout << "Yo What motha' duckin' job number is you? Ya' dig? (10, 20, or 30): ";
cin >> job;
}
}
return job;
}
int hoursWorked(int hours)
{
cout << "Yo! How geezery hours dahd ya work?: ";
cin >> hours;
return hours;
}
double jobCalc(int& job, int& hours, double& pay)
{
if (job == 10)
{
if (hours < 40)
{
pay = hours * 8.75;
}
else if (hours < 60)
{
pay = 8.75 * 40;
hours = hours - 40;
pay = pay + (hours * 13.125);
}
else
{
pay = 8.75 * 40;
pay = pay + (13.125 * 20);
hours = hours - 60;
pay = pay + (17.5 * hours);
}
}
else if (job == 20)
{
if (hours < 40)
{
pay = hours * 12.25;
}
else if (hours < 60)
{
pay = 12.25 * 40;
hours = hours - 40;
pay = pay + (hours * 18.375);
}
else
{
pay = 12.25 * 40;
pay = pay + (18.375 * 20);
hours = hours - 60;
pay = pay + (24.5 * hours);
}
}
else if (job == 30)
{
if (hours < 40)
{
pay = hours * 13.75;
}
else if (hours < 60)
{
pay = 13.75 * 40;
hours = hours - 40;
pay = pay + (hours * 20.625);
}
else
{
pay = 13.75 * 40;
pay = pay + (20.625 * 20);
hours = hours - 60;
pay = pay + (27.5 * hours);
}
}
return pay;
}
void display(string firstname, string lastname, int job, int hours, double pay)
{
std::cout << std::fixed;
std::cout << std::setprecision(2);
cout << firstname << " " << lastname << " has job " << job << " and worked " << hours << " hours, thus earning them $" << setprecision(2) << pay << "." << endl;
}
all of your functions are returning values. where you are storing it?. multiple return is not possible as you are using. try the concept of pointers(references)
I have a problem with my program and I would appreciate help.
It's supposed to allow you to enter an account number, a service code, and the number of minutes the service was used. The program then calculates the bill and it varies depending on your service. When I execute the program, it doesn't allow you to enter anything.
Regular service:$10.00 plus first 50 minutes free. Charges for over 50 minutes are $0.20 per minute.
Premium service:
$25.00 plus:
a)
For calls made from 6:00 am to 6:00 pm, the first 75 minutes are free; charges for over
75 minutes are $0.10 per minute.
b)For calls made from 6:00 pm to 6:00 am, the first 100 minutes are free; charges for over 100 minutes are $0.05 per minute.
Here is the program that I've typed.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
int minutes = 0,
day_minutes = 0, //minutes used during the day
night_minutes = 0; //minutes used during the night
string service_code,
account_number;
double final_amount,
final_damount, //final amount for day minutes
final_namount = 0; //final amount for night minutes
cout << "Please enter your account number: ";
cin >> account_number;
cout << "Please enter your service code (r or R for regular service and p or P for premium service): ";
cin >> service_code;
if (service_code == "r")
{
cout << "Please enter the amount of minutes used: " << minutes << endl;
}
if (minutes <= 50)
{
final_amount = 10;
cout << "Your final amount is $: " << final_amount << endl;
}
if (minutes > 50)
{
final_amount = (minutes * 0.20) + 10;
cout << "Your final amount is $: " << final_amount << endl;
}
else if (service_code == "p")
{
cout << "Please enter the amount of minutes used during the day: " << day_minutes << endl;
cout << "Please enter the amount of minutes used during the night: " << night_minutes << endl;
}
if (day_minutes <=75)
{
final_damount = 0;
final_amount = final_damount + final_namount + 20;
}
if (day_minutes > 75)
{
final_damount = day_minutes * 0.10;
final_amount = final_damount + final_namount + 20;
}
if (night_minutes <= 100)
{
final_namount = 0;
final_amount = final_damount + final_namount + 20;
}
if (night_minutes > 100)
{
final_namount = night_minutes * 0.05;
final_amount = final_damount + final_namount + 20;
cout << "Your final amount is: $ " << final_amount << endl;
}
else
cout << "Error, this program does not accept negative numbers.\n";
return 0;
}
Does anyone the problem to my program? Thank you.
In your code you never specify to ask for user input (read from any input streams or files), hence it doesn't ask for any input.
You will probably want to somewhere do something such as cin or cin.readline or any of several other various methods to read the user's input from stdin.
To avoid duplicating other questions I'm not putting further details here.
4.16 (Salary Calculator) Develop a C++ program that uses a while statement to determine the
gross pay for each of several employees.
When someone works 41 hours or more. They get paid 1.5x more so my problem is that in my else if statement. I did rate * 1.5 which should translate into 10 * 1.5 = 15 but I get 425 because my code on the top probably, but I don't understand it at the moment.
He gave us this example. Which I'm trying to emulate.
"
Enter hours worked (-1 to end): 41
Enter hourly rate of the employee ($00.00): 10.00
Salary is $415.00
"
#include <iostream>
using namespace std;
int main()
{
double salary=0,rate=0,hours=0,counter=0;
cout << "Enter your hours worked: ";
cin >> hours;
cout << "Enter your rate: ";
cin >> rate;
while(counter<=hours)
{ salary=hours*rate;
counter++;
}
if(hours==-1)
{exit(0);
}
else if(hours>=41)
{
rate = rate * 1.5;
salary = salary + rate;
}
cout << "$" << salary;
return 0;
}
The loop you used has no functionality related to the problem, so I omitted it. Below is what will work. As others have said, define why you need a loop. I'm guessing you need to loop the code so the user can repeat this to their heart's content. If that is the case, I'll let you try to figure out how to break out of the loop when a user enters -1.
if (hours == -1)
exit(0);
else if (hours <= 40)
salary = rate * hours;
else {
double overtimeHours = hours - 40;
salary = rate * 40;
rate = rate * 1.5;
salary = salary + (rate * overtimeHours);
}
cout << "$" << salary;