Fraction Class - Calculating Less Than Percentage GCD On Negative Numbers
Feb 15, 2014
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
For some reason, I'm getting the correct result, but my negative sign is having issues. For example, if I do 1/4 - (-2/4), I get (-3/4).
Here is my minus method for subtracting fractions.
/** Subtracts a fraction from another fraction. @param toUse, the fraction to subtract. @return minusFraction, the result after subtraction. */ public Fraction minus(Fraction toUse)
[Code] .....
Here is my reduce() method, just in case...
/** Reduces the fraction, if possible, to it's simplest form. Converts negative fractions to the form -x/y, or if -x/-y --> x/y */ private void reduce() { int lowest = Math.abs(numerator); int highest = Math.abs(denominator);
[code]...
I only switched an operator from my previous addition method, given here as well. I think only switching the + to a - may have caused my issue.
/** Adds two fractions together. @param toUse, the fraction to be added. @return plusFraction, the sum of the two fractions. */ public Fraction plus(Fraction toUse) {
Create an integer array with 10 numbers, initialize the array to make sure there are both positive and negative integers. Write a program to generate two arrays out of the original array, one array with all positive numbers and another one with all negative numbers. Print out the number of elements and the detailed elements in each array.
public class problem3 { public static void main(String[]args){ int[] numbers = {1, 2, 3, 4, 5, -1, -2, -3, -4, -5}; for (int i = 0; i<numbers.length;){ if(i>0){ System.out.println(numbers); } else System.out.println(numbers); } } }
I am trying to get the average of 3 different fraction arrays. I made a fraction class and I made methods such as read() and average() in this new class.
package fractions; import java.util.Scanner; import java.util.Arrays; public class FractionArrays { public static void main(String[] args) { Fraction completeFraction = new Fraction(5,6);
[Code] ....
I was wondering if there was any way to use the arrays I created in the read method in the average method. If I find that out I should be able to do it on my own. Is there a way to make the arrays public to use in different methods?
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.
I am very new to Java. I have been working for a couple months on a program for school. It has not gone well. I finally was able to scrap together a working program, but i left something out that needs to be. I have to include input validation to check for negative values, prompting users to re-enter values if negative. I have included my current code, the program works perfectly, but what to do about the negative numbers.
Java Code:
package gradplanner; import java.util.Scanner; public class GradPlanner { public static void main(String[] args) { Scanner input = new Scanner(System.in); int numofclasses = 0; int totalCUs = 0;
I am very new to Java. I have been working on a program. It has not gone well. I finally was able to scrap together a working program, but i left something out that needs to be. I have to include input validation to check for negative values, prompting users to re-enter values if negative.I have included my current code, the program works perfectly, but what to do about the negative numbers.
package gradplanner; import java.util.Scanner; public class GradPlanner { public static void main(String[] args) { Scanner input = new Scanner(System.in); int numofclasses = 0;
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();
My verify method also always returns false. So I'm given three classes to begin with. Calculator, Expression, and InfixExpression and they are listed below.
The goal is to create a class called PostfixExpression that extends Expression and can read and calculate postfix expressions.
My evaluate() method works for most calculations but when it needs to return a negative value it just returns the positive equivalent.
Also, my verify method always returns false and I can't pinpoint why.
Here's my current code. Some things are commented out for debugging purposes.
import java.util.Scanner; /** * Simple calculator that reads infix expressions and evaluates them. */ public class Calculator { public static void main(String[] args) { Scanner input = new Scanner(System.in);
I'm having some issues getting this code to reject negative numbers. What I'm doing wrong.
import java.util.Random; import java.util.Scanner; public class ForLoop { public static void main (String [] args) { Random randomNumber = new Random();
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...
You are to design a Java application to carry out additions and subtractions for numbers of any length. A number is represented as an object which includes a sign and two strings for the whole and decimal parts of the number. And, the operations must be done by adding or subtracting characters directly. You are not allowed to convert these strings to numbers before the operation.
The program must use a "Number" class which includes at least the following methods:
Number ( ); Number (double n); Number add (Number RHS); Number subtract (Number RHS); String toString ( );
This is what i have but it only adds positive numbers and it doesn't subtract problems like 7.05-8.96. Also some of it was what our teacher gave us like alignwhole method
import java.util.Scanner; public class Number{ private String whole; private String decimal; private String sign; public static void main (String[] args){ System.out.println("Enter two numbers");
You are to design a Java application to carry out additions and subtractions for numbers of any length. A number is represented as an object which includes a sign and two strings for the whole and decimal parts of the number. And, the operations must be done by adding or subtracting characters directly. You are not allowed to convert these strings to numbers before the operation.
The program must use a "Number" class which includes at least the following methods:
Number ( ); Number (double n); Number add (Number RHS); Number subtract (Number RHS); String toString ( );
The below code is what our teacher gave us to start with, but it needs to add and subtract positive or negative numbers of any length. This code only adds positive numbers. Need to write code for subtraction .
Java Code:
import java.util.Scanner; public class Number{ private String whole; private String decimal; private String sign;
So I have re-written the code but it is still not running correctly. Any number i type in it throws an exception, also i need the program to add the totals that i type in and then once i type -1 into the prompt button list all the number i typed in and give me the average.
import java.awt.*; import java.awt.event.*; import javax.swing .*; import javax.swing.text.*; public class Averages extends JFrame { //construct components JLabel sortPrompt = new JLabel("Sort By:");
My question is how do I configure this so that when / if I need to change the discount value, it automatically adjusts the % discount off of the Full or Compact?
All I can get to work is simply hardcoding it and assigning the discount value, i'm probably just messing up the syntax.
These are the values I have. Basically I just need an explanation of how to code a statement so I receive the calculated discount of a Full or Compact.
Here is the excerpt from the code, I can copy the whole code in too if needed.
Java Code:
System.out.println("You selected Item1."); System.out.print("Have coupon? [Y or N]"); answer = kb.next(); if (answer.equalsIgnoreCase("n")) System.out.print("Price is 30.50 per day."); if (answer.equalsIgnoreCase("y")) >This is what I'm looking for< mh_sh_highlight_all('java');
I am trying to create a program that allows me to enter 5 students numeric grade (0-100) to a letter grade (A, B, C, D, F) and I CANNOT use an array. When I try to run my program it says main class not found, and when I change the it from a string to a void in the main method it does not work.
Java Code:
import java.util.Scanner; public class Week3ControlStatements2 { public static String main(String[] args){ int numberGrade = 0; int quotient = numberGrade / 10; int remainder = numberGrade % 10;
I was wondering how you would change a double to a fraction.
For Example: double x = .5
From this we know that the fraction is obviously 1/2 (simple form of course), but what are the steps necessary to convert the double to the fraction 1/2?
With fields that holds a tax payer social security number, last name, first name, street address, city zip code, annual income, marital status and tax liability, include a constructor that requires argument that provide values for all other fields other than the tax liability. the constructor calculates the tax liability based on annual income and percentage in the ff table
Income 0-20,000 20,000-50,000 50,000 and over
marital status single married 15% 14% 22% 20% 30% 28%
I am supposed to make a code which can handle fractions as a calculator in the form of 1_1/2 + 1 = 2_1/2
This is what I've done so far and now im just lost.
Heres my code:
import java.util.Scanner; import java.util.StringTokenizer; public class FracCalc { public static void main(String[] args ){ // read in the first expression
package FracCalc; import java.util.Scanner; public class FracCalc { public static void main(String[] args) { Scanner scan = new Scanner(System.in);
int n1 = 0; //n1 = first numerator user have to enter int d1 = 0; //d1 = first denominator user have to enter int n2 = 0; //n2 = second numerator user have to enter int d2 = 0; //d2 = second denominator user have to enter int numeratorAnswer = 0; //calculates numerator of an equation int denominatorAnswer = 0; //calculates denominator of an equation
[code]...
I mean its not that important but i figure it will be better if I could print out the answer both in fraction format and decimal format. while my answer prints in fraction just fine how do i show it in decimal?
System.out.println("");
System.out.print("And "); System.out.print("answer in Decimal is: ");