Моя программа должна иметь возможность создать новую фракцию, которая имеет три конструктора и четыре метода. Однако, когда я запускаю свою программу через драйвер тестирования, она не скомпилируется с драйвером. когда я тестирую конструктор, он возвращает y/z вместо того, что я хотел бы, чтобы мой конструктор по умолчанию был равен 0/1. Большинство моих фракций даже не выходят как целое число, но все еще как переменная, которую я им тоже назначил .... почему это?Создание нового класса под названием Fraction
public class Fraction //new class name
{
//class data
private int numerator;
private int denominator;
private int y;
private int z;
//constructors
public Fraction() // default - sets numerator to 0/denominator to 1.
{
this.numerator = 0;
this.denominator = 1;
}
public Fraction (Fraction y) //PARAMETERIZED - numerator takes input Y, denominator is 1.
{
this.numerator = y;
this.denominator = 1;
}
public Fraction (int y, int z) //parametrized - throws new illegalarumentexception
{
this.numerator = y;
this.denominator = z;
if (z == 0)
throw new IllegalArgumentException(z+ " cannot equal 0.");
}
//methods
public String toString() //should return fraction in a string
{
return ("y/z");
}
public double evaluate() //returns fraction as a decimal in double
{
return (double) y/z;
}
public boolean isImproper()
{
return y > z;
}
public Fraction multiply (Fraction another)
{
return new Fraction (this.numerator*another.numerator);
}
}
Heres водитель:
**public class FractionTester
{
public static void main(String[] args)
{
//create and test Fractions
System.out.println("------ Part1: Testing Fractions's constructors and toString()");
System.out.println("\nTest1.1: Testing default constructor, then toString()");
try
{
System.out.print("expected: 0/1 \ngot: ");
Fraction f1 = new Fraction();
System.out.println(f1);
}
catch (Throwable ex)
{
System.out.println("got: " + ex);
}
System.out.println("\nTest1.2: Testing new Fraction(2) constructor, then toString()");
try
{
System.out.print("expected: 2/1 \ngot: ");
Fraction f1 = new Fraction(2);
System.out.println(f1);
}
catch (Throwable ex)
{
System.out.println("got: " + ex);
}
System.out.println("\nTest1.3: Testing new Fraction(2, 5) constructor, then toString()");
try
{
System.out.print("expected: 2/5 \ngot: ");
Fraction f1 = new Fraction(2, 5);
System.out.println(f1);
}
catch (Throwable ex)
{
System.out.println("got: " + ex);
}
System.out.println("\nTest1.4: Testing new Fraction(2, 0) constructor (bad input), then toString()");
try
{
System.out.print("expected: java.lang.IllegalArgumentException(<your descriptive String>) \ngot: ");
Fraction f1 = new Fraction(2, 0);
System.out.println(" no exception");
}
catch (Throwable ex)
{
System.out.println(ex);
}
System.out.println("\nTest1.5: Testing new Fraction(0, 2) constructor (OK), then toString()");
try
{
System.out.print("expected: 0/2 \ngot: ");
Fraction f1 = new Fraction(0, 2);
System.out.println(f1);
}
catch (Throwable ex)
{
System.out.println("got: " + ex);
}
System.out.println("\n------ Part2: Testing Fraction's methods");
System.out.println("\nTest2.1: Testing .evaluate with 3/4 (original should be unchanged)");
try
{
System.out.print("expected: 0.75 \ngot: ");
Fraction f1 = new Fraction(3,4);
String originalToString = f1.toString();
double result = f1.evaluate();
System.out.println(result);
if (!originalToString.equals(f1.toString()))
System.out.println("***** BUT THE ORIGINAL FRACTION CHANGED");
}
catch (Throwable ex)
{
System.out.println("got: " + ex);
}
System.out.println("\nTest2.2: Testing .isImproper with 7/6 (original should be unchanged)");
try
{
System.out.print("expected: true \ngot: ");
Fraction f1 = new Fraction(7,6);
String originalToString = f1.toString();
boolean result = f1.isImproper();
System.out.println(result);
if (!originalToString.equals(f1.toString()))
System.out.println("***** BUT THE ORIGINAL FRACTION CHANGED");
}
catch (Throwable ex)
{
System.out.println("got: " + ex);
}
System.out.println("\nTest2.3: Testing .isImproper with 10/13 (original should be unchanged)");
try
{
System.out.print("expected: false \ngot: ");
Fraction f1 = new Fraction(10,13);
String originalToString = f1.toString();
boolean result = f1.isImproper();
System.out.println(result);
if (!originalToString.equals(f1.toString()))
System.out.println("***** BUT THE ORIGINAL FRACTION CHANGED");
}
catch (Throwable ex)
{
System.out.println("got: " + ex);
}
System.out.println("\nTest2.4: Testing .isImproper with 7/7 (original should be unchanged)");
try
{
System.out.print("expected: false \ngot: ");
Fraction f1 = new Fraction(7,7);
String originalToString = f1.toString();
boolean result = f1.isImproper();
System.out.println(result);
if (!originalToString.equals(f1.toString()))
System.out.println("***** BUT THE ORIGINAL FRACTION CHANGED");
}
catch (Throwable ex)
{
System.out.println("got: " + ex);
}
System.out.println("\nTest2.5: Testing .multiply 4/5 times 3/11 (original should be unchanged)");
try
{
System.out.print("expected: 12/55 \ngot: ");
Fraction f1 = new Fraction(4, 5);
String originalf1ToString = f1.toString();
Fraction f2 = new Fraction(3, 11);
String originalf2ToString = f2.toString();
Fraction f3 = f1.multiply(f2);
System.out.println(f3);
if (!originalf1ToString.equals(f1.toString()) || !originalf2ToString.equals(f2.toString()))
System.out.println("***** BUT THE ORIGINAL FRACTION(S) WERE CHANGED!");
}
catch (Throwable ex)
{
System.out.println("got: " + ex);
}
} //end of main
} //end of class**
Если 'numerator' является' int', вы не можете назначить его, чтобы иметь значение 'y', который типа' Fraction' ... как в Мир - это компиляция вообще? – childofsoong
Очевидно, что эту программу невозможно скомпилировать. Сначала прочитайте и поймите, что компилятор вам говорит. Если вы не знаете, что означает сообщение, это может быть хорошим вопросом. После этого выясните, почему он не делает то, что вы думаете. –
Работайте с ошибками компиляции по одному, и не пытайтесь запустить тестер снова, пока все ваши ошибки компиляции не исчезнут. Обратите особое внимание на типы данных переменных, которые вы используете. Например, если у вас есть две переменные, типы которых - «Фракция» и «int», подумайте о том, имеет ли смысл присваивать значение одной переменной другому. –