My objective is to provide a class Stacker that contains a method stackSize that takes an int called extension as its parameter. It should return the smallest long n such that the sum of the sequence 1/2 + 1/4 + 1/6 + 1/8 + ... + ... + 1/2n is greater than or equal to extension.
Assuming my Rat class (Rational Number) correctly adds up two fractions using the first parameter as a numerator and the second parameter as a denominator, any ideas why this code isn't working for me?
The default Rat() constructor simply makes the Rational number (0/1) and the Rat(extension) constructor makes the Rational number (extension/1).
public class StackerTest {
#Test
public void test1()
{
assertEquals(4, Stacker.stackSize(1));
}
}
When I run the test case:
#Test
public void test1()
{
assertEquals(4, Stacker.stackSize(1));
}
It runs for a solid 30 seconds before giving an IllegalArgumentException
Here is the constructor for Rat class as well as the add method
/**
* If d is zero, throws an IllegalArgumentException. Otherwise creates the
* rational number n/d
*/
public Rat (int n, int d)
{
// Check parameter restriction
if (d == 0)
{
throw new IllegalArgumentException();
}
// Adjust sign of denominator
num = n;
den = d;
if (den < 0)
{
num = -num;
den = -den;
}
// Zero has a standard representation
if (num == 0)
{
den = 1;
}
// Factor out common terms
int g = gcd(Math.abs(num), den);
num = num / g;
den = den / g;
}
/**
* Returns the sum of this and r
*/
public Rat add (Rat r)
{
int firstNum = this.num*r.den;
int firstDen = this.den*r.den;
int secondNum = r.num*this.den;
int secondDen = r.den*this.den;
Rat x = new Rat(firstNum+secondNum, firstDen);
return x;
}
Related
I have a class Calculator which aggregates instances of a class Fraction as its attributes.
Class Fraction has attributes num for numerator and denom for denominator.
Here is an abstract of the code with 'multiply' and 'simplify' (to get a fraction in its lowest terms) methods.
public class Calculator {
private Fraction f1 = new Fraction(4, 9);
private Fraction f2 = new Fraction(3, 8);
public void multiply() throws Exception {
int num = f1.getNum() * f2.getNum();
int denom = f1.getDenom() * f2.getDenom();
Fraction f = new Fraction(num, denom);
simplify(f);
System.out.print(f);
}
private void simplify(Fraction f) throws Exception {
int num = f.getNum();
int denom = f.getDenom();
for (int i = num; i > 0; i--) {
if ((num % i == 0) && (denom % i == 0)) {
num = num / i;
denom = denom / i;
break;
}
}
}
However, I get 12/72 as a result of multiplication while I should get 1/6.
How can I change the code so that 'simplify' method works when invoked in 'multiply'?
As Edwin commented you want algorithm for greatest common divisor. However to answer your question, you got unexpected result because the newly computed variables num and denom at the line with num = num / i are not stored back into Fraction f.
Either (worse option) call f.setNum(num); f.setDenom(denom) or (better) change Fraction to immutable class and return new Fraction from simplify method.
I'm trying to use Junit for the first time but I'm facing some unexpected failure.
Here is the failure message:
org.opentest4j.AssertionFailedError: expected: <2> but was: <19>.
It would be great if someone will be able to help me understand where is my error.
I spend more than 30 minutes in trying to understand the reason behind it and I can't. I guess I need to do a minor change somewhere.
public class Fraction {
private int numerator;
private int denominator;
public int getNumerator() {
return numerator;
}
public int getDenomonator() {
return denominator;
}
public Fraction(int n, int d) {
numerator = n;
denominator = d;
}
/**
* This method is adding other fraction
* to our current(this) fraction
* #param otherFraction
*/
public void add(Fraction otherFraction) {
int a = numerator;
int b = denominator;
int c = otherFraction.getNumerator();
int d = otherFraction.getDenomonator();
numerator = a * d + b * c;
denominator = b * d;
int min = denominator;
if (numerator < denominator) {
min = numerator;
}
int commonDiv = 1;
for (int i = 1; i <= min; i++) {
if ((numerator % i == 0) && (denominator % 1 == 0)) {
commonDiv = i;
}
}
numerator = numerator / commonDiv;
denominator = denominator / commonDiv;
if (numerator == 0) denominator = 1;
}
}
Test:
class FreactionTest {
#Test
void test() {
Fraction f1 = new Fraction(3,4);
Fraction f2 = new Fraction(5,6);
f1.add(f2);
assertEquals(f1.getNumerator(),19);
assertEquals(f1.getDenomonator(),12);
}
#Test
void testAddNegative() {
Fraction f1 = new Fraction(3,4);
Fraction f2 = new Fraction(-3,4);
f1.add(f2);
assertEquals(f1.getNumerator(),0);
assertEquals(f1.getDenomonator(),1);
}
}
I expected the code to run successfully.
It looks like you have your expected/actual backwards in the call to assertEquals(). According to the docs here, the first argument is the expected value, and the second argument is the actual value. So you need to switch your arguments, since right now you're hard-coding the actual result to be 19. The call you're trying to test should be the second argument, and the value you expect to be returned should be the first argument. You're doing it in all your other assertEquals() calls also, so be sure to change those as well.
I need to create an array list to save the value of resistors entered by user. Then I need to ask user which type of method to calculate his answer with, then return back answer. The do while loop needs to keep asking for resistors values until user enter the number zero. The calculations have to be pulled from a class Resistance to Main.
**CLASS***
import java.util.ArrayList;
public class Resistance {
/**
* Holds an object type.
*/
public int userChoice;
ArrayList<Double> resistor = new ArrayList<Double>();
/**
*Chooses which process follows next.
*#return circuitType
*/
public final double calculateResistance() {
if (userChoice == 1) {
return calculateSeriesResistance();
} else {
return calculaParallelResistance();
}
}
/**Returns object of parallel circuit.
*
* #return 1 / runningResistance.
*/
public double calculaParallelResistance() {
double runningResistance = 0;
for (int index = 0; index < resistor.lenght; ++index) {
runningResistance = runningResistance
+ +1 / resistor.lenght;
}
return 1 / runningResistance;
}
/**Returns object of series circuit.
*
* #return runningResistance.
*/
private double calculateSeriesResistance() {
double runningResistance = 0;
for (int index = 0; index < resistor.length; ++index) {
runningResistance = runningResistance + resistor[index];
}
return runningResistance;
}
}
***MAIN********
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
/**
* Makes a Constant.
*/
public final static int DONE = 0;
/**
* Makes a Constant.
*/
public static final int USER_CHOICE_SERIES = 1;
/**
* Makes a Constant.
*/
public static final int USER_CHOICE_PARALLEL = 2;
public static void main(final String[] args) {
// TODO Auto-generated method stub
DecimalFormat f = new DecimalFormat("##.00");
Resistance myRes = new Resistance();
Scanner keyboard = new Scanner(System.in);
//Display Purpose
System.out.println("\nThis program will calculate "
+ "the resistance of a Parallel or Series "
+ "Circuit\n");
//Display instructions
System.out.println("Please enter the value of your "
+ "resistors separately, when done press 0 (zero)");
int n = 0;
do {
System.out.println("Enter Resistor #" + ++n);
myRes.resistor.add(keyboard.nextDouble());
} while (myRes.resistor > 0);
// Ask Which Method To Use,
do {
System.out.println("\nEnter 1 to calculate "
+ "a Series Circuit "
+ "or 2 for a Parallel Circuit");
myRes.userChoice = keyboard.nextInt();
// Ask user again in case he enters something else
} while (myRes.userChoice != USER_CHOICE_PARALLEL
&& myRes.userChoice != USER_CHOICE_SERIES);
//Output the total resistance
System.out.printf("\nThe Total Resistance is "
+ f.format(myRes.calculateResistance()));
}
}
As said, there are few isues with your code, let's break it down step by step.
First, ArrayList doesn't have a field called length, you want to use size() instead.
Next, your method of calculating parallel resistance is wrong. You are not iterating over your resistors, instead you're using number of resistors to calculate. The correct formula is:
1 / Rtotal = 1 / R1 + 1 / R1 + 1 / R3 + ...
Change your code to:
public double calculaParallelResistance() {
double runningResistance = 0;
//use size() instead of length, which doesn't exist in ArrayList
for (int index = 0; index < resistor.size(); index++) {
//also, iterate over each resistor's value, and to get it,
//use...err, get() method :)
runningResistance += (1 / resistor.get(index));
}
return 1 / runningResistance;
}
Your method calculateSeriesResistance() also has an issue: you can't access object by using array notation, as in resistance[index]. Use get() method, so the correct code is:
private double calculateSeriesResistance() {
double runningResistance = 0;
for (int index = 0; index < resistor.size(); index++) {
runningResistance += resistor.get(index);
}
return runningResistance;
}
Now, to the main method. Your first 'do' loop is checking against number of resistors in your ArrayList, not against 0 value entered by user. You could use while loop instead and break when 0 entered:
//holds current resistor's value
Double val;
while(true) {
System.out.println("Enter Resistor #" + ++n);
val = keyboard.nextDouble();
//only add if higher than 0
if (val > 0.0) {
myRes.resistor.add(val);
}
//0 entered, so finish the loop
else break;
}
So what I'm trying to do is convert double to rational number. I check how many digits there is after decimal point and I want to save the number 123.456 as 123456 / 1000, for example.
public Rational(double d){
String s = String.valueOf(d);
int digitsDec = s.length() - 1 - s.indexOf('.');
for(int i = 0; i < digitsDec; i++){
d *= 10;
}
System.out.println((int)d); //checking purposes
}
However, for the number 123.456 I get a round off error and the result is 123455. I guess it'd be possible to fix this with BigDecimal but I can't get it to work. Also, having calculated what rational number it would be, I would like to call another constructor with parameters (int numerator, int denominator) but I can't obviously call the constructor in the line where println is now. How should I do this?
For the first part of the question, Java is storing .6 as .5999999 (repeating). See this output:
(after first multiply): d=1234.56
(after second multiply): d=12345.599999999999
(after third multiply): d=123455.99999999999
One fix is to use d = Math.round(d) immediately after your loop finishes.
public class Rational {
private int num, denom;
public Rational(double d) {
String s = String.valueOf(d);
int digitsDec = s.length() - 1 - s.indexOf('.');
int denom = 1;
for(int i = 0; i < digitsDec; i++){
d *= 10;
denom *= 10;
}
int num = (int) Math.round(d);
this.num = num; this.denom = denom;
}
public Rational(int num, int denom) {
this.num = num; this.denom = denom;
}
public String toString() {
return String.valueOf(num) + "/" + String.valueOf(denom);
}
public static void main(String[] args) {
System.out.println(new Rational(123.456));
}
}
It works - try it.
For the second part of your question...
In order to call the second constructor from the first, you can use the "this" keyword
this(num, denom)
But it has to be the very first line in the constructor... which doesn't make sense here (we have to do some calculations first). So I wouldn't bother trying to do that.
This code may be overkill for you, but it deals with the rounding error that you're experiencing, and it also takes care of repeating decimals (4.99999999999999 turns into 5, and 0.33333333333333333333 turns into 1/3).
public static Rational toRational(double number){
return toRational(number, 8);
}
public static Rational toRational(double number, int largestRightOfDecimal){
long sign = 1;
if(number < 0){
number = -number;
sign = -1;
}
final long SECOND_MULTIPLIER_MAX = (long)Math.pow(10, largestRightOfDecimal - 1);
final long FIRST_MULTIPLIER_MAX = SECOND_MULTIPLIER_MAX * 10L;
final double ERROR = Math.pow(10, -largestRightOfDecimal - 1);
long firstMultiplier = 1;
long secondMultiplier = 1;
boolean notIntOrIrrational = false;
long truncatedNumber = (long)number;
Rational rationalNumber = new Rational((long)(sign * number * FIRST_MULTIPLIER_MAX), FIRST_MULTIPLIER_MAX);
double error = number - truncatedNumber;
while( (error >= ERROR) && (firstMultiplier <= FIRST_MULTIPLIER_MAX)){
secondMultiplier = 1;
firstMultiplier *= 10;
while( (secondMultiplier <= SECOND_MULTIPLIER_MAX) && (secondMultiplier < firstMultiplier) ){
double difference = (number * firstMultiplier) - (number * secondMultiplier);
truncatedNumber = (long)difference;
error = difference - truncatedNumber;
if(error < ERROR){
notIntOrIrrational = true;
break;
}
secondMultiplier *= 10;
}
}
if(notIntOrIrrational){
rationalNumber = new Rational(sign * truncatedNumber, firstMultiplier - secondMultiplier);
}
return rationalNumber;
}
This provides the following results (results from test cases are shown as comments):
Rational.toRational(110.0/3.0); // 110/3
Rational.toRational(11.0/1000.0); // 11/1000
Rational.toRational(17357.0/33300.0); // 17357/33300
Rational.toRational(215.0/21.0); // 215/21
Rational.toRational(0.123123123123123123123123); // 41/333
Rational.toRational(145731.0/27100.0); // 145731/27100
Rational.toRational(Math.PI); // 62831853/20000000
Rational.toRational(62.0/63.0); // 62/63
Rational.toRational(24.0/25.0); // 24/25
Rational.toRational(-24.0/25.0); //-24/25
Rational.toRational(-0.25333333333333333333333); // -19/75
Rational.toRational(-4.9999999999999999999999); // -5
Rational.toRational(4.9999999999999999999999); // 5
Rational.toRational(123.456); // 15432/125
It's not elegant, however, I believe this does what you're asking.
double a = 123.456;
String aString = Double.toString(a);
String[] fraction = aString.split("\\.");
int denominator = (int)Math.pow(10, fraction[1].length());
int numerator = Integer.parseInt(fraction[0] + "" + fraction[1]);
System.out.println(numerator + "/" + denominator);
Here, d=123.456 then num=123456, j=1000.
/**
* This method calculates a rational number from a double.
* The denominator will always be a multiple of 10.
*
* #param d the double to calculate the fraction from.
* #return the result as Pair of <numerator , denominator>.
*/
private static Pair<Integer,Integer> calculateRational(double d){
int j=1, num;
do{
j=j*10;
}while((d *j)%10!=0);
j=j/10;
num=(int)(d*j);
return new Pair<>(num,j);
}
Here're some tests:
#Test
public void testCalculateRational() {
Assert.assertEquals(new Pair<>(124567, 1000), calculateRational(124.567));
Assert.assertEquals(new Pair<>(12456, 100), calculateRational(124.56));
Assert.assertEquals(new Pair<>(56, 100), calculateRational(0.56));
}
Try
for(int i = 0; i <= digitsDec; i++){
}
With the help of some very nice people from this forum I've been able to translate some c++ into java language, but I'm not sure how to call this classes. Bassically what they are supposed to do is to return a "gen4 style" curve. If someone have an idea how to get this running please let me know!
/*
Derived from gen4 from the UCSD Carl package, described in F.R. Moore,
"Elements of Computer Music." It works like setline, but there's an
additional argument for each time,value pair (except the last). This
arg determines the curvature of the segment, and is called "alpha" in
the comments to trans() below. -JGG, 12/2/01
http://www.music.columbia.edu/cmc/rtcmix/docs/docs.html (maketable/gen4)
trans(a, alpha, b, n, output) makes a transition from <a> to <b> in
<n> steps, according to transition parameter <alpha>. It stores the
resulting <n> values starting at location <output>.
alpha = 0 yields a straight line,
alpha < 0 yields an exponential transition, and
alpha > 0 yields a logarithmic transition.
All of this in accord with the formula:
output[i] = a + (b - a) * (1 - exp(i * alpha / (n-1))) / (1 - exp(alpha))
for 0 <= i < n
*/
import java.lang.Math;
private static final int MAX_POINTS =1024;
public class gen{
int size; /* size of array to load up */
int nargs; /* number of arguments passed in p array */
float []pvals; /* address of array of p values */
double []array; /* address of array to be loaded up */
int slot; /* slot number, for fnscl test */
}
public static void fnscl(gen g) {
}
static void trans(double a, double alpha, double b, int n, double[] output) {
double delta = b - a;
if (output.length <= 1) {
output[0] = a;
return;
}
double interval = 1.0 / (output.length - 1);
if (alpha != 0) {
double denom = 1 / (1 - Math.exp(alpha));
for (int i = 0; i < output.length; i++)
output[i] = a + (1 - Math.exp(i * alpha * interval)) * delta * denom;
} else {
for (int i = 0; i < output.length; i++)
output[i] = a + i * delta * interval;
}
}
public static double gen4(gen g) {
int i;
int points = 0;
int seglen = 0;
double factor;
double time [] = new double[MAX_POINTS];
double value [] = new double[MAX_POINTS];
double alpha [] = new double[MAX_POINTS];
double ptr [];
if (g.nargs < 5 || (g.nargs % 3) != 2) /* check number of args */
System.out.println("gen4 usage: t1 v1 a1 ... tn vn");
if ((g.nargs / 3) + 1 > MAX_POINTS)
System.out.println("gen4 too many arguments");
for (i = points = 0; i < g.nargs; points++) {
time[points] = g.pvals[i++];
if (points > 0 && time[points] < time[points - 1])
System.out.println("gen4 non-increasing time values");
value[points] = g.pvals[i++];
if (i < g.nargs)
alpha[points] = g.pvals[i++];
}
factor = (g.size - 1) / time[points - 1];
for (i = 0; i < points; i++)
time[i] *= factor;
ptr = g.array;
for (i = 0; i < points - 1; i++) {
seglen = (int) (Math.floor(time[i + 1] + 0.5)
- Math.floor(time[i] + 0.5) + 1);
trans(value[i], alpha[i], value[i + 1], seglen, ptr);
ptr[i] += seglen - 1;
}
fnscl(g);
return 0.0;
}
If I understand your question correctly and you want to execute your program, you need some adjustments to your code.
You need to have a class. To execute it, you need a special main method.
/**
*Derived from...
*/
import java.lang.Math;
class Gen4Func {
class Gen {
// insert from question
}
public static void main(String[] args) {
// prepare parameters
// ...
// call your function
trans( ... );
// do more stuff
// ...
}
public static void fnscl(gen g) {
}
static void trans(double a, double alpha, double b, int n, double[] output) {
// insert from above
}
}
Save this to Gen4Func.java (must match class name). Then execute from via
> java Gen4Func
As I said: If I understand your question correctly.
HTH,
Mike
Bulky, copypaste to Gen.java and try setting Gen class fields with test values in main() method.
/*
* Derived from gen4 from the UCSD Carl package, described in F.R. Moore,
* "Elements of Computer Music." It works like setline, but there's an additional
* argument for each time,value pair (except the last). This arg determines the
* curvature of the segment, and is called "alpha" in the comments to trans()
* below. -JGG, 12/2/01
*
* http://www.music.columbia.edu/cmc/rtcmix/docs/docs.html (maketable/gen4)
*
*
*
* trans(a, alpha, b, n, output) makes a transition from <a> to <b> in <n>
* steps, according to transition parameter <alpha>. It stores the resulting <n>
* values starting at location <output>. alpha = 0 yields a straight line, alpha
* < 0 yields an exponential transition, and alpha > 0 yields a logarithmic
* transition. All of this in accord with the formula: output[i] = a + (b - a) *
* (1 - exp(i * alpha / (n-1))) / (1 - exp(alpha)) for 0 <= i < n
*/
public class Gen {
private static final int MAX_POINTS = 1024;
int size; //size of array to load up
int nargs; //number of arguments passed in p array
float[] pvals; //address of array of p values
double[] array; //address of array to be loaded up
int slot; //slot number, for fnscl test
public static void main(String[] args) {
Gen g = new Gen();
//initialize Gen fields here..
Gen.gen4(g);
}
public static void fnscl(Gen g) {
}
public static void trans(double a, double alpha, double b, int n, double[] output) {
double delta = b - a;
if (output.length <= 1) {
output[0] = a;
return;
}
double interval = 1.0 / (output.length - 1);
if (alpha != 0) {
double denom = 1 / (1 - Math.exp(alpha));
for (int i = 0; i < output.length; i++) {
output[i] = a + (1 - Math.exp(i * alpha * interval)) * delta * denom;
}
} else {
for (int i = 0; i < output.length; i++) {
output[i] = a + i * delta * interval;
}
}
}
public static double gen4(Gen g) {
int i;
int points = 0;
int seglen = 0;
double factor;
double time[] = new double[MAX_POINTS];
double value[] = new double[MAX_POINTS];
double alpha[] = new double[MAX_POINTS];
double ptr[];
if (g.nargs < 5 || (g.nargs % 3) != 2) /*
* check number of args
*/ {
System.out.println("gen4 usage: t1 v1 a1 ... tn vn");
}
if ((g.nargs / 3) + 1 > MAX_POINTS) {
System.out.println("gen4 too many arguments");
}
for (i = points = 0; i < g.nargs; points++) {
time[points] = g.pvals[i++];
if (points > 0 && time[points] < time[points - 1]) {
System.out.println("gen4 non-increasing time values");
}
value[points] = g.pvals[i++];
if (i < g.nargs) {
alpha[points] = g.pvals[i++];
}
}
factor = (g.size - 1) / time[points - 1];
for (i = 0; i < points; i++) {
time[i] *= factor;
}
ptr = g.array;
for (i = 0; i < points - 1; i++) {
seglen = (int) (Math.floor(time[i + 1] + 0.5)
- Math.floor(time[i] + 0.5) + 1);
trans(value[i], alpha[i], value[i + 1], seglen, ptr);
ptr[i] += seglen - 1;
}
fnscl(g);
return 0.0;
}
}
In Java, stand alone methods are not allowed. You should make them member of some class. In your case, it seems that 2 of your methods are using class gen as argument fnscl() amd gen4(). You can make them as member methods. The other one can remain static within class.
public class gen{
// data ...
public void fnscl () { ... } // member method
public double gen4 () { ... } // member method
// static method
public static void trans(double a, double alpha, double b, int n, double[] output) { ... }
}
main() also should be part of some class. I leave that choice up to you.
Java is a fully object oriented language in terms of paradigms. (Not as c++ which is also procedural), that's why as Kerrek SB said all methods has to be declared and defined inside a class.
What he was mistaken is that a file can contain only one public class and it has to be named exactly as the file. But it can also contain any number non-public classes with arbitrary names.
To run the program, you have to first compile the file with javac [filename].java then run it with java [classname] without! .class
One more thing. You can't declare a method like this:
public void foo();
The compiler will consider it to be an abstract method and raise an error message.