Identifier Expected Error In Method Writing - java

public class Rational{
public Rational (int numerator, denominator){
if (denominator < 0){
system.out.println("Denominator cannot be negative value. Changing rational to have positive denominator...");
denominator = -denominator;
numerator = -numerator;
}
if (denominator == 0){
system.out.println("Denominator cannot be zero, reinput denominator.");
int n = numerator;
int d = denominator;
}
}
//end of initialization
//behaviors
public Rational inverse(int n, d){
if (numerator == 0){
int temp = numerator;
numerator = denominator;
denominator = temp;
}
else system.out.println("Error, the inverse results in division by zero.");
}
public Rational simplify(int n, d){
if (n%d == 0){
return n;
}
else if (n < d){
return simplify (d, n);
}
else return simplify(d, n%d);
numerator = numerator / n;
denominator = denominator /n;
}
}
New Java student here, and I'm running into an "indentifier expected" error on lines 2, 16, and 24. All those lines are public Rational, and the error points to the closing parentheses specifically. I've tried looking through similar questions on here, but I can't figure out what's wrong with mine. From what I saw the error has to do with defining methods outside of a block, but it looks like it's in one to me. Any help would be appreciated!

There are a couple of problems with your program:
You need to declare the type before each parameter for a function. This is what is causing the error message you are getting For instance, you need:
public Rational inverse(int n, int d){
and
public Rational (int numerator, int denominator){
You need to capitalize s in System.
You need to declare the numerator and denominator fields within the Denominator class. For instance:
public class Rational{
int numerator;
int denominator;

Related

How can I return null of denominator is 0 in this method Java

I am struggling with creation of a to calculate fractions and return in an array[3]. So if nominator is 7 and denominator is 3 it should return {2,1,3}, a fraction part and a integer part. If denominator is 0, the integer division should not be executed and I have to return null (not print it). That is what I am struggling with. My code is below:
public class Main{
public static int[] fraction(int nominator, int denominator){
int quota=0;
int numerator=0;
try{
quota = nominator / denominator;
numerator = nominator % denominator;
}
catch(java.lang.ArithmeticException e)
{
if(denominator==0)
{
quota =0;
numerator=0;
}
}
int [] integerArray ={quota, numerator,denominator};
return integerArray;
}
}
As mentioned in the comments, you can just return early once you checked the denominator is 0.
public static int[] fraction(int nominator, int denominator){
if (denominator == 0){
return null
}
int quota = nominator / denominator;
int numerator = nominator % denominator;
int [] integerArray = {quota, numerator,denominator};
return integerArray;
}
you can proceed in 2 different ways:
you can control if denominator is 0 before the try and return null instantly (as said in the comments)
you used try catch so you can return null into the catch block without further controls because you catch an exception
there should not be any compilation errors so if you get one try to read the console and find what's the problem.

Java How to get 0 as denominator to show in rational class

Right now, when running my Rational program I get a 1 when creating a new rational with the numerator being 2 and denominator being 0, and my teacher wants me to replace the denominator with a 1 instead of a 0 but I am unable to and get a stack overflow error whenever trying to add an if in the reduce method that is used to reduce the fractions to their simplest form. I have also tried adding an if statement to the constructor where the rational object containing the numerator and denominator are, but still to no avail. I also have a similar problem when a rational is created with a 0 numerator and 2 in the denominator. It is supposed to return 0/1, but 0 is returned. I believe these two are related, does anyone know what's going on here?
public Rational(int numerator, int denominator) {
int gcd = reduce(numerator, denominator);
num = numerator / gcd;
denom = denominator / gcd;
if(denom == 0)
{
denom = 1;
}
}
private static int reduce(int numerator, int denominator) {
if(denominator == 0) numerator = 1;
return reduce(denominator, numerator % denominator);
}
I don't fully understand the quest and cannot speak to the rational portion of your problem but the stack overflow error is caused by your function reduce. The issue is that there is no endpoint for this function and because it is recursive, function calls will build up on the stack until there is no more space available thus resulting in a stack overflow. The following change will fix the stack overflow error.
private static int reduce(int numerator, int denominator) {
if(denominator == 0) { //assuming this is the end point
numerator = 1;
return numerator; //this ends the recursive call
}
return reduce(denominator, numerator % denominator);
}
If you're looking for a method that finds the greatest common denominator then the following change to your function should work.
private static int reduce(int numerator, int denominator) {
if(denominator == 0) {
return numerator;
}
return reduce(denominator, numerator % denominator);
}

how to correctly modify integers with while loops

So basically, I'm just trying to return a GCD from two previous numbers,
public static String GCD( int Denominator,int Numerator) {
int newNum=Numerator;
int newDen=Denominator;
int newWhole=Whole;
int GCD=0;
if (Numerator == 0) {
GCD = Denominator;
}
while (Denominator != 0) {
if (Numerator > Denominator) {
Numerator = Numerator - Denominator;
}else {
Denominator = Denominator - Numerator;
}
}
GCD = Denominator;
System.out.println(GCD);
}
You would think that since i declared GCD outside the loop that it would return just fine
but no, i just get 0 as an answer? almost every time..... thoughts?
That's because your while loop is dependent on the value of Denominator as the loop ends when the value of Denominator is decreased to 0.
And right outside the while loop, you're storing the value of Denominator in 'GCD' which is obviously '0'.
So no matter what you do in the while loop, you would get 0 as the final result of your GCD.
I hope this solves your problem.
You need a return statement somewhere in your code. Probably at the end of the method, you need to write return GCD;.

GCD of Large Numbers

I'm working on a project that involves using a system of equations to solve for certain radii. My method for that seems to be working fine, but to return the correct answer I need to return a ratio in the simpilest form. So 24/2 would come back as 12/1, which is stored in an array. So the final answer would be [12,1].
For numbers that work nicely I have no difficulty. But there are times when there will be radii that are 843.667 and I need to return this as the most basic fraction. And this is where I'm getting stumped because I cant figure out how to do it. Either I get a lossy conversion error, the wrong number, or just zeros.
//Class set-up to be given a double and return both its numerator and denominator
class Rational {
public int num, denom;
//Establishes both the numerator and denominator
public Rational(int num, int denom) {
this.num = num;
this.denom = denom;
}
public Rational(double d) {
//Split up the number, so that we have the integer value and decimal value
long i = (long) Math.ceil(d);
double numerator = d - i;
// Know how many decimal places we are dealing with and establish a proper numerator and denominator
String frac = new Double(numerator).toString();
frac = frac.substring(frac.indexOf('.'));
numerator = Double.parseDouble(frac);
int power = frac.length();
double denominator = Math.pow(10, power);
//Find the GCD of the numerator and denominator
double gcd = findGCD((int) numerator, (int) denominator);
numerator /= gcd;
denominator /= gcd;
this.num = (int) numerator;
this.denom = (int) denominator;
}
// Method to find the GCD of two int values
public static int findGCD(int n1, int n2) {
while (n1 != n2) {
if (n1 > n2) {
n1 = n1 - n2;
} else {
n2 = n2 - n1;
}
}
return n1;
}
}
If you can help figure out how my 843.667 could be turned into a fraction, that would be amazing.

why does this value not change when I turn it into a negative?

I am trying to write a class Rational that had a few methods relating to adding, subtracting, etc. I want to make it so that within the constructor, I add the values to the private variables and find the GCD to find simplify the fraction. The problem I run into is with my if statements. I want to check if the numbers within the object parameter are negative so I use the if statement to check. The only problem is when I run the program, it doesn't give me a negative value i.e. I have Rational p = new Rational(-24, 48) and it only returns 1/2.
public class TestRational {
public static void main(String... args) {
Rational p = new Rational(-24, 48);
}
public Rational(long a, long b){
numerator = a;
denominator = b;
boolean isNegative = false;
if (numerator*denominator < 0)
isNegative = true;
long gd = gcd(numerator, denominator);
numerator /= gd;
denominator /= gd;
if (isNegative)
numerator = -numerator;;
}
private long gcd(long p, long q){
//checks to see if numerator greater than denominator
if(p<q)
return gcd(q,p);
if(Math.abs(q) == 0)
return p;
long remainder = Math.abs(p)%Math.abs(q);
return gcd(Math.abs(q), Math.abs(remainder));
}
}
You dont need this
if (isNegative)
numerator = -numerator;;
So the constructor becomes
public Rational(long a, long b){
numerator = a;
denominator = b;
boolean isNegative = false;
if (numerator*denominator < 0)
isNegative = true;
long gd = gcd(numerator, denominator);
numerator /= gd;
denominator /= gd;
}
Hope it works ...
Unless your question asked you explicitly to use GCD and the range of a and b is not big, you can implement it simply with a loop:
public Rational(long a, long b){
boolean isNegative = a < 0 || b < 0;
a = Math.abs(a);
b = Math.abs(b);
for (int i = min(a, b); i >= 2; --i)
if (a % i == 0 && b % i == 0)
{
a /= i;
b /= i;
}
numerator = isNegative ? -a : a;
denominator = b;
}
i would like point some "avoidable mistakes" in your code.
Name of constructor must be same as name of class. In your case it is not.
use blocks for if{ statements} even if it contains single statement.
you have not declared type of local variable numerator and denominator
what private variables you are talking about ?
Pay little more attention to code while posting, it will help you to get good answers to your question

Categories