Click here to Skip to main content
15,877,591 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
what does the code display while it is executed ?
please explain why the answer is :
10/8
17/12
3/2 
15/9 
 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 Fraction.main(Fraction.java:33)

here is the program:
Java
public class Fraction {
 private int numerator;
 private int denominator; 

public Fraction(int n, int d) {
numerator = n; denominator = d;} 

public Fraction add(Fraction f) { 
return newFraction(numerator ∗ f.denominator + f.numerator ∗ denominator, denominator ∗ f.denominator);} 

public Fraction add(int x) {
 return newFraction(numerator + x ∗ denominator, denominator);} 

public Fraction add(int n, int d) {
 return newFraction(numerator ∗ d + n ∗ denominator, denominator ∗ d);} 

public String toString() { return numerator + "/" + denominator;} 

public static void main(String[] args) { 

Fraction f1 = newFraction(1, 2);
Fraction f2 = newFraction(2, 3); 
Fraction f3 = newFraction(3, 4);

Fraction[] sums = newFraction[4]; 
sums[0] = f1.add(f3); 
sums[1] = f2.add(f3); 
sums[2] = f1.add(1); 
sums[3] = f2.add(3, 3);

 for (int i = 0; i <= sums.length; i++) { 
System.out.println(sums[i].toString());
}
}
}


What I have tried:

......................................................................
Posted
Updated 20-Apr-18 19:44pm
v3

The message
Quote:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 Fraction.main(Fraction.java:33)
is telling you that you're trying to access beyond the bounds of an array. Specifically, the index value 4 is the problem.
The line
for (int i = 0; i <= sums.length; i++) { 
has the error, but I'll leave you to think it through.
 
Share this answer
 
Comments
Member 13676546 20-Apr-18 20:57pm    
thanks.
but the array is initialized as 4 , so why does it give this exception
Peter_in_2780 20-Apr-18 21:14pm    
The array has 4 elements, yes. They are numbered 0, 1, ... Now you figure it out.
Not your question, but when one play with fractions, it is a common practice to reduce the fractions
10/8  => 5/4
17/12
3/2 
15/9 => 5/3

the reduction is done by dividing both numbers with their GCD.
-----
Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
Some coding styles are easier to read than other, see your code:
Java
public class Fraction {
	private int numerator;
	private int denominator;

	public Fraction(int n, int d) {
		numerator = n; denominator = d;
	}

	public Fraction add(Fraction f) {
		return newFraction(numerator ∗ f.denominator + f.numerator ∗ denominator, denominator ∗ f.denominator);
	}

	public Fraction add(int x) {
		return newFraction(numerator + x ∗ denominator, denominator);
	}

	public Fraction add(int n, int d) {
		return newFraction(numerator ∗ d + n ∗ denominator, denominator ∗ d);
	}

	public String toString() { return numerator + "/" + denominator;
	}

	public static void main(String[] args) {

		Fraction f1 = newFraction(1, 2);
		Fraction f2 = newFraction(2, 3);
		Fraction f3 = newFraction(3, 4);

		Fraction[] sums = newFraction[4];
		sums[0] = f1.add(f3);
		sums[1] = f2.add(f3);
		sums[2] = f1.add(1);
		sums[3] = f2.add(3, 3);

		for (int i = 0; i <= sums.length; i++) {
			System.out.println(sums[i].toString());
		}
	}
}

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
 
Share this answer
 
Comments
Member 13676546 21-Apr-18 8:57am    
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900