I need to make graph-like matrix when a user enters their paychecks for all 12 months of year. Simplegraph will print output, rounding entered paycheck to nearest 1000.
To be specific, my output needs to look like the picture below. For example: if a user enters a value between 1501 and 2500, it would be rounded to 2000.
I have figured out how to round the values, but I can't figure out how to place an X for the correct value in the appointed month.
I suppose that I need to save the values in an array. However, if I save them in an array, I can't figure out how to place them in their appropriate month.
one entry per month in the array?
when looping and creating the matrix just have a method like
for y
for x
if(shouldPutX(x,y,months))
System.out.print("X")
else
System.out.print(" ")
shouldPutX(x,y,months){
int monthVal = month[x-1]
if monthVal should be rounded to this line
return true
else
return false
}
// creating scanner with 12 spaces (12 months)
int[] salaryIntArray = new int[12];
Scanner salary = new Scanner(System.in);
// counters
int i = 0;
int j = 0;
// entering salary for every month in array
while (i<salaryIntArray.length){
System.out.println("Enter syour salary:");
salaryIntArray [i] = salary.nextInt();
i++;
}
// trying to round numbers, but it doesnt work with arrays, only when I'am using simple integer variables
// Rules to round numbers:
// 0 - 500 round to 0
// 501 - 1500 round to 1000
// 1501 - 2500 round to 2000
// 2501 - 3500 round to 3000
// 3501 - 4500 round to 4000
// 4501 - 5500 round to 5000
int roundNumberArray = ((salaryIntArray + 499) / 1000 * 1000);
// System.out.println(roundedNumber);
// System.out.println();
// Test print to check to see if salaries are correctly saved
System.out.println();
int month = 1;
for (int j = 0; j<12; j++){
System.out.println("Salary for " + month + " month is " + salaryIntArray[i1]);
month ++;
}
What I need now is to how to make table/matrix
Related
Problem:
Write a program that creates a conversion table from kilograms to pounds. The program should prompt the user for the starting point of the table in kilograms and list ten entries in increments of one kilogram. The conversion factor is 1 kilogram is equal to 2.2 pounds.
I need to use a for-loop for this program. I tested with inputs 20, 21, 22.
My output is all messed up, the 20 is over the kilograms column when it should be below. The last input, 22, is ending the program when it should let me enter all numbers between 20 and 30 before it ends. The column pounds is not being calculated. Is there something I'm missing?
The output link is
*/import java.util.Scanner;
public class kgToLbs1
{
public static void main(String[] args) {
final double LB_PER_KG = 2.2;
//Algorithm
//Open keyboard for input
Scanner input = new Scanner(System.in);
//Prompt user for beginning value and assign to begVal
System.out.println("Enter beginning value ===> ");
int begVal = input.nextInt();
//Print conversion table headings
System.out.println("Kilograms Pounds");
int kg = input.nextInt();
double lb = input.nextDouble();
//endVal = begVal + 9
int endValue = begVal + 9;
for (int i = 20; i <= 30; i++) {
System.out.printf("%d%.2f\n", i , (i * LB_PER_KG));
}
//Print conversion table footer
System.out.println("End");
//Close terminal
it should let me enter all numbers between 20 and 30
No, that's not what the instructions say. It says prompt for any starting value, then increment up by one for the next ten values. In other words, print only, not prompt for input
the 20 is over the kilograms column when it should be below
There is a 20 below. There just isn't a space after it because the printf didn't include a space
The column pounds is not being calculated
It is. 2044.00 is printing 20 (kg) and 44.00 (lbs) without a space
I think you're looking for this
final double LB_PER_KG = 2.2;
Scanner input = new Scanner(System.in);
System.out.print("Enter beginning value ===> ");
int begVal = input.nextInt();
System.out.println("Kilograms\tPounds");
for (int i = begVal; i <= begVal + 10; i++) {
System.out.printf("%d\t%.2f\n", i , (i * LB_PER_KG));
}
I'm trying to make a game show program where I enter the number of contestants, how fast they hit the buzzer, the fastest and slowest times for hitting the buzzer, and the number of people that hit the buzzer faster than average.
Everything goes well with my program when I have 5 contestants. But if I enter 4 contestants or anything else less than 5, it messes up some things. When I have 4 contestants, my fastest time is 0, even when I'm not putting in a 0 as a time, and the number of contestants that hit the buzzer faster than average is also messed up, having one extra than it should, because it's putting in a 0.
How do I stop this program from adding in a 0 when I don't have 5 contestants?
import java.util.Scanner;
public class FastestFingers {
public static void main(String[] args) {
//declare varialbes
int contestants;
int [] milisecs = new int [6];
int fastest, slowest, i;
int faster = 0;
double average;
Scanner scanInt = new Scanner (System.in);
//make user enter number of contestants
System.out.println ("Enter # of contestants: ");
contestants = scanInt.nextInt();
//make user enter times
for(i=1;i<=contestants;i++)
{
System.out.println ("Time (ms): ");
milisecs[i] = scanInt.nextInt();
}
//calculate fastest time
fastest = milisecs[1];
for(i=1;i<milisecs.length;i++)
{
if(milisecs[i] < fastest)
{
fastest = milisecs[i];
}
}
System.out.println ("Fastest: " + fastest);
//calculate slowest time
slowest = milisecs[1];
for(i=1;i<milisecs.length;i++)
{
if(milisecs[i] > slowest)
{
slowest = milisecs[i];
}
}
System.out.println ("Slowest: " + slowest);
//tell program how to find average
int total = 0;
for(i=1;i<milisecs.length;i++)
{
total = total + milisecs[i];
}
average = total/contestants;
//find numbers faster than the average
int count = 0;
for(i=1;i<milisecs.length;i++)
{
if(milisecs[i]<average)
{
count++;
}
}
System.out.println ("Faster than average: " + count);
}
}
You create an int[] of fixed size 6. Then you compare the last 5 values to find the lowest. All int values are default 0. If you only set 4 values to anything larger than 0 the fifth value will always be 0 and therefore your lowest value.
You can fix this problem by setting the size of your array to the number of contestants.
Also remember that arrays start counting at 0 not at 1. Your loops should therefore be:
for(i=0;i<milisecs.length;i++), etc.
The problem is that you initialize the array to have six elements.
int [] milisecs = new int [6];
By default all of these values will be zero. Then in your loop you only initialize four of them, meaning that two will still be zero. Change your code to:
System.out.println ("Enter # of contestants: ");
contestants = scanInt.nextInt();
int [] milisecs = new int [contestants];
This will ensure that you will only have as many time slots as you have contestants.
Also array indexes start at zero, so you should change
fastest = milisecs[1];
to
fastest = milisecs[0];
And in your loops, start your variable at 0 instead of 1.
Array Index should start with 0. Please be careful with this. You code reads input and assigns to the milisecs starting from index 1.
I wrote a program that prompts the user to enter 10 grades, and it calculates the sum of the grades, the average of grades, the highest grade, smallest grade and the range between highest and smallest. And then it prints all these things using system.out.print.
My problem is that everything works EXCEPT for the highest value. Here is my code.
int x = 10;
int i = 1;
int sum = 0;
int guess = 100;
int max = 100;
while (i <= 10) {
x = Integer.parseInt(JOptionPane.showInputDialog("Enter one grade at a time."));
i++;
sum+=x;
if(x<guess){
guess = x;
}
if(x>max){
max = x;
}
}
System.out.println("The sum of your grades is " + sum);
System.out.println("The average of your grades is " + (sum/10));
System.out.println("The smallest grade is " +guess);
System.out.println("The highest grade is " +max);
System.out.println("The range of your grades is " + (max-guess));
Lets say the 10 numbers entered are 10,20,30,40,50,60,70,80,90,99 it will print saying "The highest grade is 100" but 100 wasn't entered. It always says 100. How can I fix this? Thank you!!
Set max to be 0 instead of 100:
int max = 0;
You're initially setting max to 100. Which means that at the end of the last iteration (where x is equal to 99), this condition
if(x > max)
would be false.
(Notice how 100 is greater than any of the numbers that you use in your test sample. If there was a number greater than 100 included, this wouldn't have been the case, & your code would seem to work.)
It looks like you initialized max with 100, so it will always be greater than x and not changed. Change the declaration of max to be zero and it should work.
Because in initial variable int max = 100; you put the default value as 100(maximum), so compiler checks if the value entered is less than 100, which is as default. It is no. so max variable keeps value 100 as always, unless higher input is in
I am showing the declining balance of a loan per year and the interest paid per year. The first and second year are always messed up, causing the rest of the years to not be correct. Also, how do I print a column with designated space for each entry, besides using spaces to separate them like I did?
My code:
int year;
periods = loanDurationYears*12;
double annualBalance = 0;
double annualInterest = 0;
int month = loanDurationYears-(loanDurationYears-1);
balance = loanAmount;
double interestForMonth = balance*((interestRate*.01)/12);
double principalForMonth = monthlyPayment - interestForMonth;
balance = balance - principalForMonth;
System.out.println("Annual balances");
System.out.printf("%s %s %s \n","Year","Interest","Balance");
for(int j=0;j<periods;j++)
{
month++;
year = month/12;
interestForMonth = balance*((interestRate*.01)/12);
principalForMonth = monthlyPayment - interestForMonth;
balance = balance - principalForMonth;
annualBalance = annualBalance + balance;
annualInterest = annualInterest + interestForMonth;
if(month%12 == 0)
{
System.out.printf("%d %.2f %.2f \n",year,annualInterest,annualBalance);
annualBalance = 0;
annualInterest = 0;
}
}
My output:
Year Interest Balance
1 4852.43 859718.74
2 5080.12 899718.26
3 4842.34 857208.50
4 4588.01 811738.89
5 4315.96 763103.31
6 4024.98 711081.35
7 3713.73 655437.20
8 3380.81 595918.66
9 3024.71 532255.97
10 2643.82 464160.58
11 2236.40 391323.84
12 1800.62 313415.64
13 1334.49 230082.85
14 835.91 140947.76
15 302.62 45606.39
How the output should look: (besides the columns not being aligned)
Year Interest Loan Balance
1 5965.23 86408.21
2 5715.14 82566.33
3 5447.64 78456.94
4 5161.51 74061.43
5 4855.46 69359.87
6 4528.10 64330.94
7 4177.95 58951.87
8 3803.41 53198.26
9 3402.80 47044.03
10 2974.29 40461.31
11 2515.95 33420.24
12 2025.70 25888.91
13 1501.31 17833.19
14 940.40 9216.58
15 340.45 -0.00
You are getting an incorrect starting amount because you adjust the balance before the for-loop. It looks like your for-loop expects to start with the full balance, but it's already been reduced. You should also either move the increment of month to the end of the loop, or start it at zero instead of 1.
This line will always initialize month to 1. Why bother with the math?
int month = loanDurationYears-(loanDurationYears-1);
I haven't tested this or anything but it should work. I'm sure it's not the best fix but something simple like this should work fine.
It simply checks the size of the variable and then uses different print statements depending on the size of it. Also as the year throws off alignment I've put some code that should fix that
if(month%12 == 0)
{
// Set the amount of decimals for all interest rates
DecimalFormat df = new DecimalFormat("#.##");
df.format(annualInterest);
// Get the length of annualInterest
int n = math.floor(annualInterest);
int length = (int)(Math.log10(n)+1);
if (length = 3)
{
// Have 1 less space than normal on the print statement
// Maybe also do a check on the year also as that throws it out when it goes past 10
if (year > 10)
System.out.printf("%d %.2f %.2f \n",year,annualInterest,annualBalance);
else
System.out.printf("%d %.2f %.2f \n",year,annualInterest,annualBalance);
}
if (length = 2)
{
// Have 2 less spaces than normal
}
annualBalance = 0;
annualInterest = 0;
}
Okay so I changed my code around and deleted a lot of the unnecessary garbage in it. It works for some numbers but not for others, for example, when I put in 100 rolls/8 sides/3 die it gives me an out of bounds error despite the limits I've set for it. Obviously I've looked over some detail, I'm just not sure what detail it is.
public class Ass11f {
public static void main(String[] args) {
EasyReader console = new EasyReader();
System.out.print("Enter how many times you want to roll the die: ");
int numRolls = console.readInt();
System.out.print("Enter the amount of sides: ");
int numSides = console.readInt();
System.out.print("Enter the amount of die: ");
int numDie = console.readInt();
int[] rollSum = new int[numDie*numSides];
for (int i = 0; i<numRolls; ++i)
{
int rollCounter=0;
for (int l = 0; l<numDie; ++l){
rollCounter += ((int)(Math.random()*numSides)+1);
}
rollSum[rollCounter]++;
}
for (int m = 2;m<=rollSum.length;++m) System.out.println(m+"'s: "+rollSum[m]+" times, "+((((double)rollSum[m])/numRolls)*100)+"%");
}
}
There are two base problems:
When adding roll totals, you're trying to add the maximum roll in an index one past the end of the array. The easy fix is to simply add 1 to the length of your array.
When printing, you cannot access an array using an index equal to the array's length, which is what m<=rollSum.length will eventually do. Replace that with m < rollSum.length so it stops before the final value.
Also, here's some ways to make your array creation a bit clearer:
// The minimum value is always numDie.
// The maximum is always numDie * numSides
// There are maximum - minimum + 1 possible values (ie 6 on a d6)
int maximum = numDie * numSides;
int minimum = numDie;
// Remember, index zero is now the minimum roll.
// The final index is the maximum roll. So the count at an index is really
// the count for any roll with value index + minimum
int[] rollSum = new int[maximum - minimum + 1];
I also recommend splitting up that print statement. It's a bit easier to read and debug. Also, you can start at numDie instead of 2 to account for when you have more or less die than 3:
for (int i = numDie; i < rollSum.length; ++i) {
// Print the first bit, ie "2's: ".
System.out.print(i + "'s: ");
// How many times was that value rolled?
System.out.print(rollSum[i] + " times, ");
// What percentage is that?
double percentage = ((double)rollSum[i]) / numRolls * 100;
System.out.println(percentage + "%");
}