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) {
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;
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
I have started learning Java and have some across some difficulties. I'm trying to subtract two strings.
for example, with these strings;"032"&&"100". I want to be able to subtract each number individually so that the answer would be "032". 0-1;3-0;2-0;. if i get a negative number, the number would be zero. hence, the 0-1 staying zero in the answer.
I have tried using substring, and parsing the two values to ints, but don't know what to do next. I have also tries using a for loop, to go through each arrays of the strings. I am not allowed to use StringBuilder and I need to return "032".
Budget program. Here is my situation, I have 2 tabs in a GUI, one tab adds a transactions when the add button is clicked, and in the other tab displays a table showing all the transactions. In my code, I want it so that when the user chooses a deposit(combo box variable name = cbType, indexnumber for deposit is 0) it will add to the total and when the user chooses withdraw(index number is 1) then it will subtract it from the total. Here is the code.... (note as well, the code also adds a new row to the table)
So when I tested the program with 2 transactions, the first transaction was a deposit and the 2nd transaction was a withdraw. The end product was that both amounts were subtracted from the total. When I did a withdraw first and a deposit second, the amounts were both added together.
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?
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?
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: ");
So all the errors in the code is fixed but its now got the wrong output
what I get:
Enter numerator; then denominator. 5 8 5/8 Enter numerator; then denominator. 4 10 4/10 Sum:0/0
it should be:
Enter numerator; then denominator. 5 8 5/8 Enter numerator; then denominator. 4 10 4/10 Sum: 82/80 1.025 Product: 20/80 0.25 Enter numerator; then denominator. 6 0 infinity
here is the code
Fraction.java public class Fraction { //declares that it's a class that can be intantiated private int numer; // private intager for numerator private int denom; // private intager for denominator
We're learning how to use Binary I/O commands...which equates to....
My issue is trying to relate the Fraction objects (which we are to create using a loop) with the read/write methods used in Binary I/O (input/output streams). I left a blank after the output.write(), so you can see where the issue exist.
Java Code:
import java.io.*; public class FractionTest { public static void main(String[] args) { int [] fraction = new int[3]; for(int i = 0; i <= fraction.length; i++){ Fraction numbers = new Fraction();
when i input a positive integer it works but when i input a negative number it doesn't work
my pseudo code:
READ input WHILE( NOT CORRECT INPUT) READ INPUT AGAIN; ENDWHILE DECLARE array arr[input] FOR(i=0 to input-1) arr[i]= Random number from 0 to 100; ENDFOR DISPLAY ARRAY
error message when i input -5 : Exception in thread "main" java.lang.NegativeArraySizeException atPosNeg.main<PosNeg.java:36>
import java.util.*; class PosNeg{ public static void main(String args[]) { Random generator = new Random(); Scanner scan = new Scanner(System.in);
I am new to Android. I have byte array of size 10. I am passing the Decimal values (131 - 140) to byte array. But while printing I get Negative (-) values with decreasing order .
How can I get same value as positive values?
Or How can I store positive value e.g. 131 as byte array element.
Please not my requirement is array must be ByteArray only
I am not sure what is happening with my code, but it is giving me a negative number. I am trying to write a program that calculates the product of the odd integers between 1 and 25. I messed with the program and as soon as you enter a number over 22, the end result is a negative number.
int total = 1; for (int i = 1; i <= 25; i += 2){ total *= i; } System.out.println("Product:" + total);
I am trying to raise a real number to a negative value e.g x-y(x raised to power -y) which can also be written like 1/xk . I am trying to run th program each time bt it doesnt give me the desired result.
Here is the code
public class RaiseRealPower { public void run(){ double value =readInt("Enter value "); double power =readInt("Enter power "); System.out.println("Answer is " +raiseIntToPower(value,power));
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;
Ask the user to enter a sequence of at most 20 nonnegative integers. Your program should have a loop that reads the integers into an array and stops when a negative is entered (the negative number should not be stored). Invoke the average method to find the average of the integers in the array (send the array as the parameter).
how can I remove the negative number from the array and calculate the average of the posive elements without the negative ones? This is my code so far...
import java.util.Scanner; import javax.swing.JApplet; public class Parameters { //------------------------------------- //Calls the average and minimum methods //with different numbers of parameters
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;
How would I go about inputting the negative values in the array in case 1 the array comes from the user, case 2 from the text file? The sum prints perfectly fine with positive values but if I input negative values it just completely ignores them.
case 1: int sum; System.out.print("Enter list of comma-delimeted integers: "); Scanner scan = new Scanner(System.in); String input2=scan.next(); String[] num = input2.split(","); int[] a= new int[num.length];