My methods aren't bringing the data along with it (beginner) [duplicate] - java

This question already has answers here:
Division in Java always results in zero (0)? [duplicate]
(3 answers)
Closed 8 years ago.
This is my first CS project ever. After creating my method, I ran the code to see if it works so far, everything works correctly but it does not actually do the math inside the method. Been working on this forever and can't find the bug. Any help would be great. Its due tomorrow lol.
public static void main(String[] args) {
int numberOfStudents = 0;
int total = 0;
int value = 0;
int creditHours;
double tuition;
int classesMissed;
System.out.println("Tuition Wasted Based on Student Absences and its effect on GPA.");
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the number of students to consider: ");
value = keyboard.nextInt();
while (value >= 5)
{
if (value > 5)
System.out.println("Number of students must be between 1 and 5");
System.out.print("Please re-enter a value number of students to consider: ");
value = keyboard.nextInt();
}
System.out.print("Enter the student ID for student 1: ");
value = keyboard.nextInt();
System.out.print("For how many credit hours is the student registered: ");
creditHours = keyboard.nextInt();
System.out.print("Enter the amount of the tuition for the semester: ");
tuition = keyboard.nextDouble();
System.out.print("Enter the average number of classes the student misses in a week: ");
classesMissed = keyboard.nextInt();
while (classesMissed > creditHours)
{
if (classesMissed > creditHours)
System.out.print("That is not possible, please re-enter the number of classes missed in a week: ");
classesMissed = keyboard.nextInt();
}
DetermineWastedTuition(creditHours, tuition, classesMissed);
}
public static void DetermineWastedTuition(int creditHours, double tuition, int classesMissed){
double weeklyTuition;
weeklyTuition = tuition / 10;
double weeklyTuitionWasted;
weeklyTuitionWasted = weeklyTuition *(classesMissed / creditHours);
double semesterWasted;
semesterWasted = weeklyTuitionWasted * 16;
System.out.println("Tuition money wasted each week is " + weeklyTuitionWasted);
System.out.println("Tuition money wasted each semester is " + semesterWasted);
}
With the sample output as:
Tuition Wasted Based on Student Absences and its effect on GPA.
Enter the number of students to consider: 1
Enter the student ID for student 1: 1234555
For how many credit hours is the student registered: 15
Enter the amount of the tuition for the semester: 7500
Enter the average number of classes the student misses in a week: 2
Tuition money wasted each week is 0.0
Tuition money wasted each semester is 0.0

The following calculation :
weeklyTuitionWasted = weeklyTuition * (classesMissed / creditHours);
would return 0.0 if classesMissed < creditHours, since you are dividing two int variables, and therefore the result will be an int.
Change it to :
weeklyTuitionWasted = weeklyTuition * ((double) classesMissed / creditHours);

Related

Using java if-else [closed]

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 4 years ago.
Improve this question
This is my first time to use stack overflow to ask a question. I'm a beginner of Java programming. I was stuck on my assignment, can anyone help me to solve?
So, the problem is using java if-else to write the contents on the image into java code. But I didn't get the question. Can anyone explain? Is it possible to code using if-else? Thank you.
import java.util.Scanner;
public class MailOrderHouse{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
double product1;
double product2;
double product3;
double product4;
double product5;
System.out.println("Product price: ");
double product_price = sc.nextDouble();
System.out.println("Enter quantity sold: ");
int quantity = sc.nextInt();
}
}
I totally don't understand the question.
First, indicate to User the products available and their respective price:
int productChoice = 0;
int quantity = 0;
double totalSum = 0.0;
System.out.println("Welcome To The Mail_Order House.");
System.out.println("Please select Product Number (1 to 5) you want to buy:\n");
System.out.println("1) Product Name 1: RM2.98");
System.out.println("2) Product Name 2: RM4.50");
System.out.println("3) Product Name 3: RM9.98");
System.out.println("4) Product Name 4: RM4.49");
System.out.println("5) Product Name 5: RM6.87");
This allows the User to easily see what is available to buy and therefore make a valid choice. Now ask the User to enter a product number:
productChoice = sc.nextInt();
The value the User supplies relates to the product name he/she wants. Now it's just a matter of asking the User the desired quantity of that specific product:
System.out.println("What quantity of Product #" + productChoice + " do you want?");
quantity = sc.nextInt();
Now that we have the product quantity it's a matter using IF/ELSE IF to gather the price of that selected product and multiply it by the User supplied quantity to achieve the total sum owed for that product:
if (productChoice == 1) {
// ......TO DO........
}
else if (productChoice == 2) {
totalSum += 4.50 * quantity;
// This is the same as: totalSum = totalSum + (4.50 * quantity);
}
else if (productChoice == 3) {
// ......TO DO........
}
else if (productChoice == 4) {
// ......TO DO........
}
else if (productChoice == 5) {
// ......TO DO........
}
else {
System.out.println("Invalid product number supplied!");
}
As you can see, you now have all the required data to display the required output String to Console:
System.out.println("Mail-Order House sold " + quantity +
" of Product #" + productChoice + " for: RM" +
String.format("%.2f", totalSum));
The String.format("%.2f", totalSum) in the above line ensures a precision of 2 decimal places in the total sum is displayed to console. You wouldn't want a number like: 21.422000522340 to be displayed as a monetary value in this particular case (read up on the String.format() method).
You have to take 5 inputs for number of products sold & 5 inputs for product prices. You have to calculate the total price of these products. Instead of taking 10 variables for 10 inputs you can just use a loop like:
import java.util.Scanner;
public class MailOrderHouse{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
double total = 0;
int totalProduct = 0;
for (int i = 0; i < 5; i++) {
int productQuantity = sc.nextInt();
double productPrice = sc.nextDouble();
total += productPrice;
totalProduct += productQuantity;
}
System.out.println("Mail-order house sell " + totalProduct + " product " + totalProduct + " for RM" + productPrice);
}
}
Couldn't understand your input format though. Hope it helps.

Payroll Array Java Calculation

Hello guys, this is my first time i post something in here and i just started learning java. This is my assignment and i need to write a payroll code with array. However, i dont understand why i cant get it to work. Somehow, it only calculate the last employee, the first and second are not included. If you guys can help i'd appreciate it. Thank you!
public class ArrayIG
{
public static void main(String[] args)
{
final int NUM_EMPLOYEES = 3;
//creating array
int[]hours = new int[NUM_EMPLOYEES];
int[] employeeID = {5678459, 4520125, 7895122};
double payRate;
double wages = 0;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter your " + NUM_EMPLOYEES + " employees work hours and pay rate:");
//get the hours
for (int i = 0; i < NUM_EMPLOYEES; i++)
{
System.out.print("Employee #" + employeeID[i] + ": ");
hours[i] = keyboard.nextInt();
//get the hourly pay rate
System.out.print("Enter the pay rate: ");
payRate = keyboard.nextDouble();
wages = hours[i] * payRate;
}
//display wages
System.out.println("The hours and pay rates you entered are:");
for(int i = 0; i < NUM_EMPLOYEES; i++)
{
System.out.printf("The total wages for Employee #%d is $%.2f\n", employeeID[i], wages);
}
}
}
MY OUTPUT:
Enter your 3 employees work hours and pay rate:
Employee #5678459: 35
Enter the pay rate: 21
Employee #4520125: 37
Enter the pay rate: 18.5
Employee #7895122: 39
Enter the pay rate: 37.25
The hours and pay rates you entered are:
The total wages for Employee #5678459 is $1452.75
The total wages for Employee #4520125 is $1452.75
The total wages for Employee #7895122 is $1452.75
Either create an array of wages or calculate wages in loop where wages are being print. And you should do assignments on your own 😀
You're collecting 3 different hours but only storing them in one value. The same for the wages. What happens when you store them as an array?
import java.util.Scanner;
public class ArrayIG
{
public static void main(String[] args)
{
final int NUM_EMPLOYEES = 3;
//creating array
int[] hours = new int[NUM_EMPLOYEES];
int[] employeeID = {5678459, 4520125, 7895122};
double[] payRate = new double[NUM_EMPLOYEES];
double[] wages = new double[NUM_EMPLOYEES];
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter your " + NUM_EMPLOYEES + " employees work hours and pay rate:");
//get the hours
for (int i = 0; i < NUM_EMPLOYEES; i++)
{
System.out.print("Employee #" + employeeID[i] + ": ");
hours[i] = keyboard.nextInt();
//get the hourly pay rate
System.out.print("Enter the pay rate: ");
payRate[i] = keyboard.nextDouble();
wages[i] = hours[i] * payRate[i];
}
//display wages
System.out.println("The hours and pay rates you entered are:");
for(int i = 0; i < NUM_EMPLOYEES; i++)
{
System.out.printf("The total wages for Employee #%d is $%.2f\n", employeeID[i], wages[i]);
}
}
}
You have 3 employees -> 3 wages.
But currently you're using only one variable to hold the wage: double wages = 0;
Hence its value is replaced for every loop.
You should create an array of length 3 to store the wages:
and in your loop, replace
wages = hours[i] * payRate;
With
wages[i] = hours[i] * payRate;
And print:
System.out.printf("The total wages for Employee #%d is $%.2f\n", employeeID[i], wages[i]);
You are setting the wage rate at each iteration. I.e. you are only ever recording a single state of wages. Then you are iterating and displaying that one wages variable, which will always be the last calculation.
Store each "wages" value in an array like you have done with hours and you should resolve your issue.

Yearly price increase loop? Stuck on nested loop

I'm trying to write a program that uses scanner keyboard to input these values and uses a loop to have them increase (2006-2010, year-year, & an increase in price, 90-100, etc.)
The output is supposed to look like this:
Enter the current Mars bar price in cents: 90
Enter the expected yearly price increase: 15
Enter the start year: 2006
Enter the end year: 2010
Price in 2006: 90 cents
Price in 2007: 105 cents
Price in 2008: 120 cents
Price in 2009: 135 cents
Price in 2010: 150 cents
This is the code I have so far:
import java.util.Scanner;
public class Problem_2 {
public static void main(String[] args) {
// TODO, add your application code
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the current Mars bar price in cents: ");
int m = keyboard.nextInt();
System.out.print("Enter the expected yearly price increase: ");
int p = keyboard.nextInt();
int a = m+p;
int count = 1;
System.out.print("Enter the start year: ");
int start = keyboard.nextInt();
System.out.print("Enter the end year: ");
int end = keyboard.nextInt();
for (int i = start; i <= end; i++){
System.out.print("Price in " +i+ ": " +m);start++;
}
}
and it allows me to have the year increasing to the set amount (again like 2006-2010) but I'm stuck on having the price increase along with it by the set amount, I assume it would be a nested loop but I'm not sure how to write it.
(I assume a nested loop because that's what we're learning right now.)
If anyone has any advice on what I could write, I'd really appreciate it!
I think you just need to sum the expected yearly price increase to the current price in each iteration of your loop, it'd be something like this:
import java.util.Scanner;
public class Problem_2 {
public static void main(String[] args) {
// TODO, add your application code
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the current Mars bar price in cents: ");
int m = keyboard.nextInt();
System.out.print("Enter the expected yearly price increase: ");
int p = keyboard.nextInt();
int count = 1;
System.out.print("Enter the start year: ");
int start = keyboard.nextInt();
System.out.print("Enter the end year: ");
int end = keyboard.nextInt();
for (int i = start; i <= end; i++){
System.out.print("Price in " +i+ ": " +m);
m += p; // sum expected price for each iteration
}
}

copying arrays from a method to main method

I am asked to write a program that requires three arrays in the main method. The program is supposed to calculate gross wages for given employee ids from user input of the pay rate and hours also from user input. Also, it is asking me to write a method calculateWages with three array parameters: hours, payRate, and wages, which calculates and stores the wages for each employee by multiplying the corresponding hours and pay rates.
Your program should display each employee number and ask the user to enter that employee's hours and pay rate. After getting the hours and pay rates for all employees, your program should call calculateWages. Next, your program should display each employee's identification number and gross wages. Note: the number of employees and the minimum wage should both be named constants.
public static void main(String[] args)
{
double[] employeeID = {5658845, 4520125, 7895122, 8777541, 8451277,1302850, 7580489}; //Employee IDs of which we are calculating gross wages for
double[] payRate = new double[7];
double[] employeeWages = new double[7];
double[] employeeHours = new double[7];
Scanner keyboard = new Scanner(System.in); //Needed for keyboard input
for (int i = 0; i<employeeID.length; i++)
{
System.out.println("Employee ID: " + employeeID[i]);
System.out.print("Enter the number of hours worked: ");
employeeHours[i]=keyboard.nextDouble();
//Get and validate hours from user.
while(employeeHours[i]<0)
{
System.out.println("Error. Hours worked must not be negative.");
System.out.print("Please enter hours worked: ");
employeeHours[i]=keyboard.nextDouble();
}
//Get and validate pay rate from employees.
System.out.print("Enter the pay rate of employee: ");
payRate[i]=keyboard.nextDouble();
while(payRate[i]<10.24)
{
System.out.println("Error. Minimum pay rate must be at least $10.24");
System.out.print("Please enter the pay rate: ");
payRate[i]=keyboard.nextDouble();
}
}
calculateWages(employeeWages, employeeHours, payRate);
System.out.println("Gross Wages:");
System.out.println();
for(int i=0;i<employeeID.length;i++)
{
System.out.printf("%-9d%-7s",employeeID[i],employeeWages[i]);
}
}
public static String[] calculateWages(double[] employeeWages, double[] inputHours,double[] payRate)
{
String[] formatWage = new String[7];
DecimalFormat formatter = new DecimalFormat("$###.00");
for(int i = 1;i<employeeWages.length;i++)
{
employeeWages[i]=inputHours[i]*payRate[i];
formatWage[i]=formatter.format(employeeWages[i]);
}
return formatWage;
}
}
calculateWages is returning an array so set an array equal to it
String [] result = calculateWages(employeeWages, employeeHours, payRate);

Simple calculation not displaying correct value

I wrote a simple program that receives several inputs and it does a future investment calculation.
However, for some reason, when I enter the following values:
investment = 1
interest = 5
year = 1
I get Your future value is 65.34496113081846 when it should be 1.05.
import java.util.*;
public class futurevalue
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("This program will calculate your future investment value");
System.out.println("Enter investment amount: ");
double investment = sc.nextDouble();
System.out.println("Enter annual interest amount: ");
double interest = sc.nextDouble();
System.out.println("Enter number of years: ");
int year = sc.nextInt();
double futureValue = investment * (Math.pow(1 + interest, year*12));
System.out.println("Your future value is " + futureValue);
}
}
Found my mistake. I divided my interest twice.
You should divide your interest by 100.
How is the interest rate being entered? Should you not divide it by 100 before adding 1 inside Math.pow?
Example: Monthly interest = 1%, if you enter 1, your Math.pow would be Math.pow(1+1, year*12) which would be incorrect.
Yes, your main error is not dividing by 100 to convert from percents to proportion, but you have another error:
If you have an APR of 5% the formula you need to use to calculate a compounding monthly interest isn't 5%/12, it's
(0.05+1)^(1/12)-1
And then the return for that investment ends up being:
1 * ( (0.05+1)^(1/12)-1 +1 )^(1 * 12) =
1 * ( (0.05+1)^(1/12) )^(12) =
1 * ( 0.05+1 ) = 1.05
exactly.

Categories