Adding Fractions - Loop For Calculating Total Of Series Of Numbers
Mar 8, 2014
I'm just not noticing why it won't display the answer. I'm trying to solve this book problem......
"Write a for loop that calculates the total of the follower series of numbers:
1/30 + 2/29 + 3/28......+30/1"
Here is what I have..
public static void main(String[] args) {
double total = 0;
for (double a = 1, b = 30; b < 1; a++, b--) {
total += (a / b);
}
System.out.println(total);
}
}
When launched, the output is 0.0. I tried changing the variables a and b to doubles but didn't change anything...
So basically, I've been trying to create a for loop that asks the user for a number between 1 and 30, then calculates the total of the series of numbers. For example, if 15 is entered, the calculation should be 1/15+2/14+3/13+...15/1 which would equal approximately 38.1. The problem I'm having is that the code doesn't loop whenever I type a number for some reason, which results in a very incorrect calculation. Here is my code so far:
import java.util.Scanner; public class HmwLoop { public static void main(String[] args) { double sum = 0; for (double num1 = 1, num2 = 30; num1 <= 30 && num2 >= 1; num1++, num2--)
i want it to calculate my earnings say for 4 months and then output the result. it runs the loop asking for the 4 months worth of earnings but then it shows just the amount entered for the month and not total.
package Earnings; import java.util.Scanner; import java.lang.Math; public class Earnings { public static void main(String[] args) {
[code]....
i tried changing from double month to String month but it doesn't work.
what I'm missing to calculate the Total Payout that Payroll has given out to two employees. the professor states that we have to use "getTotalPayout" . It would have been easy to do "(employee1.getFinal() + employee2.getFinal())" but he use getTotalPayout.
public class Payroll { private String employeeId; private int hourlyrate, hoursworked; private int increaseHours = 10; private double TotalPayout;
Why is my program not calculating the total and average of the price of the items and not printing it out?
Java Code: /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package assignment4;
[code]...
It prints out everything but the total of the items and I dont know why. :(
My project is writing a program that generates a series of numbers to appear like a social security number (XXX-XX-XXXX). My code runs.. But any number below 10 it just shows one number (XXX-X-XXXX). What do I need to enter in to my code so that if the number is <10 it will show (00,01,02,03....)?
Java Code:
import java.util.Random; public class IDnumber { public static void main (String[] args) { Random generator = new Random() int num1 = (generator.nextInt(7) + 1) * 100 + (generator.nextInt(8) * 10) + generator.nextInt(8); int num2 = generator.nextInt(74); int num3 = generator.nextInt(10000); String IDnumber = num1 + "-" + num2 + "-" + num3; System.out.println(IDnumber); } } mh_sh_highlight_all('java');
write a java program to check divisibility of a series of numbers that a user inputs the starting and ending numbers and also the number to check divisibility with.
The problem I am having is it only checks the first number and doesn't check the rest.
import java.util.Scanner; public class MyMidTermAssignment { public static void main(String[] args) { int numberInputBeginning,numberInputEnding,divisibleByNumber; System.out.println("Enter first number of series");
I have to get the user to enter in 10 numbers and with those 10 numbers I have to find the total, average, smallest, largest numbers in the set the user inputs. I have the total and average already figured out but how would you go about trying to find the largest and smallest numbers within this set of code.
import java.util.*; public class testhw7 { public static void main (String [] args) { Scannernumberin = new Scanner (System.in); doublevalue[]; doubletotal;
Allow the user to specify the number of terms (5 terms are shown) to use in the computation. Each time around the loop only one extra term should be added to the estimate for PI.
Alter your solution from part one so that the user is allowed to specify the precision required between 1 and 8 digits (i.e. the number of digits which are correct; e.g. to 5 digits PI is 3.14159), rather than the number of terms. The condition on the loop should be altered so that it continues until the required precision is obtained. Note that you need only submit this second version of the program (assuming you have it working).
So I have a program for a project that permutes a random series of numbers using Java's Rand function. Basically I use a seed and a number of items I want in the arrangement of numbers, and the program makes a permutation with no two numbers being repeated. Only arrays can be applied to it, so I've been hard at work finding a solution.
Here's the code so far:
public static int permutation[]; public static void permute(int numOfItems, int seed){ permutation=new int[numItems]; Random rand = new Random(seed); permutation[0]=rand.nextInt(numItems-1);
[Code] ......
So basically I'm wanting the program to make a randomized list of numbers from the number of items I pass to the Permute method with no duplicates. I'm having some level of success with what I have written, as it gives me a randomized list when printing the output, but for some reason the first for statement in code never terminates fully, but instead runs indefinitely when generating the last integer.
For example, if I want to put 10 with a seed of 0 into it and make a list from 0-9, it will print 74283510, which is only 8 different integers. permutation[0] is manually set at the beginning of the method, which accounts for one more, but that's still only a list of 9, so I'm just wondering why the last integer is not being generated and why the program keeps looping and not terminating? I'm know for sure it's something I'm overlooking.
I'm trying to change the code on a Fibonacci series program that would allow me to exit the loop early if I exceed a specified number. The user enters any 2 random numbers (which will be the 1st 2 no.'s of the Fibonacci sequence printed to screen) and then continues up to a 'limit' on the number of numbers set in code. Here's the code:
int[] array = new int[limit]; //Define an array whose length is set by an int value for limit!! array[0] = x; //User supplies a int value for x which takes the 1st position in the array!! array[1] = y; //...and an int value for y in the 2nd position!! for (int i = 2; i < limit; i++) //Start from the 3rd position of the array when carrying out calculations!! { array[i] = array[i-1] + array[i-2];
[Code] ....
To exit the code/ 'limit' early if the array prints a number higher than 100, I tried putting a 'while' condition before the last line, as follows:
while (array[i] < 100) System.out.print(array[i] + " ");
Can I even use a 'while' loop within an array, or is there some other way I need to integrate it?
Alright, so i'm working on this and what I want to do is calculate all of the answers when the values of the correct answer = 1 and the incorrect answer = -1 so at the end of the test I can calculate answer + answer2 + answer3 and so on to get a total score...How do I do this? I've been looking online for 3 hours and i'm just stumped.
import java.util.Scanner; class HorticultureQuiz { public static void main(String[] args){ String name; String major; String confidence;
[Code] ....
I thought I could make each answer = to 1 but then I would need a -1 if the answer was incorrect so I tried this.
char answer = sc.nextLine().toLowerCase().charAt(0); if (answer == 't' || answer == 'T') answer = 1; { System.out.print("Great Job, that is correct!"); } else answer = -1; { System.out.print("Correct answer is false"); }
What I am trying to do on jtable is when a user enters values of numbers in the table, by click of button, those numbers are calculated and display in another column. For example, 4 = 2 + 2 in a table.This means getting inputs from user and summing them up. My getRowCount() and getValueAt() methods don't seem to work for me recently.
This is what I've tried so far. What i want to do is to calculate all Total Amounts by adding all the rows (5th column only),as 5th column has total amount for 1 product, and set the text for a JTextField whenever the 5th column of any row changes its value.But this isn't working my way. I have also tried keyListener to trigger that change at Enter key typed but that also don't work for me.
For some reason, the averages aren't calculating correctly. I'm aware I need to convert to double if I'm dividing by an odd amount but that itself isn't the issue. It's just simple easy divisible numbers that are not being calculated correctly. For instance, (10 + 10 + 10 + 50) / 4 = should be 2 but I get 10.
Here is the code:
import java.util.Scanner; public class Avg { public static void main(String[] args){ Scanner inputGains = new Scanner(System.in); int userInput = inputGains.nextInt();
I've been writing a fraction class code below that does a number of arithmetic calcs and when I run it these are the results I get. My gcd doesn't work when it comes to negative fractions and I'm not quite sure how to print.out the boolean methods ((greaterthan)), ((equals))and ((negative)). I'm also not sure if I have implemented those 3 methods properly. I'm still learning how to do unit testing.
Enter numerator; then denominator. -5 10 -5/10 Enter numerator; then denominator. 3 9 1/3 Sum: -5/30 -0.16666666666666666 Product: -5/30 -0.16666666666666666 Devide: -15/30 -0.5 subtract: -45/90 -0.5 negative: 1/6 0.16666666666666666 Lessthan: 1/6 0.16666666666666666 greaterthan: 1/6 0.16666666666666666
FRACTION CLASS
import java.util.Scanner; public class Fraction { private int numerator; //numerator private int denominator; //denominator
Here is my problem: if I want to add a number of X consecutive values in an array and get the minimal total value along with the indexes that caused this result, how can I do that?
For example:
X = 2 array[4] = (5,2,8,6,7)
start adding every 2 consecutive values as following:
I have a for each loop that outputs the Mechanics salary for the first team in the arraylist, how can I add these figures together instead of having separate value for each?The code in my testing class to get the salary for mechanics:
Java Code: for (Mechanic str1: formula1.get(0)){ System.out.println(str1.getSalary()); } mh_sh_highlight_all('java');
How can I also get the salary for the driver 1 + 2 in the same team and add their salary to this? I have attaached an image showing the classes/fields created
The code is below and the problem I am having is the numbers after the decimal won't add together for the total I am teaching myself java through liang's 10th edition book and I took one of the sample programs further than what was shown. The one in the book only shows the tax amount I simply wanted to make it so that the purchase amount and the tax amount would add together
package javalearning; import java.util.Scanner; public class FindingTax { public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.print("Enter Purchase amount: ");
So I've attempted a for loop for this type of problem:If you receive $0.01 on the first day of your allowance, and the daily rate doubles each day after, calculate the total earnings for 30 days.
Output should be like: Day ----- Daily allowance - Total allowance 1 -------- 0.01 -------------- 0.01 2 -------- 0.02 -------------- 0.03 3 -------- 0.04 -------------- 0.07
My code so far:
public class Allowance { public static void main (String[] args) { int day = 0; double daily = 0.0; double total = 0.1; System.out.println("Day Daily Allowance Total Allowance"); for(day = 0; day <= 30; )
for my assignment I am to only add the sort and total inventory methods in the inventory class. And not create an array in the inventory class at all. My inventory class should contain all the variables needed to use with the two new methods: sort and calculate total inventory.By not creating any array in the inventory class and not touching the variables at all.
What I have came up with is:
public class inventory { private int prodNumber; // product number private String prodName; // product name private int unitsTotal; // total units in stock private double unitPrice; // price per unit private double totalInventory; // amount of total inventory // initialize four-argument constructor
[Code] ....
The error message I get is:
F:Java Programminginventory.java:62: error: cannot find symbol return prodName.compareTo ( s. getprodName() ); ^ symbol: method getprodName() location: variable s of type inventory 1 error
Here is my program. I have to add the outputs from variable 'done' together. It seems to not work inside or outside the loop.
//A program that will find the last/check digit of a Book ISBN number.
import java.util.*; public class ISBN { public static void main(String[] args) { Scanner kb = new Scanner(System.in); System.out.println("Enter the ISBN number>"); String isbn = kb.nextLine(); //Even though the ISBN code is a number,