7. Without using your computer, what is the output of the following code int[] a
ID: 3914455 • Letter: 7
Question
7. Without using your computer, what is the output of the following code
int[] a = { 1, -2, 4, 7, 23 };
for (int k=1; k<a.length; k++)
a[k]=a[k-1];
for (int x: a)
System.out.println(x);
8. Write code that will sort an array of Rational objects from Lab xx. Test your code with the values
Rational[ ] fracs = new Rational[5];
fracs[0] = new Rational(1, 2);
fracs[1] = new Rational(1,3);
fracs[2] = new Rational(7,8);
fracs[3] = new Rational(1, 100);
fracs[4] = new Rational(5, 16);
Explanation / Answer
Please find the explanation for the problem::7
//Analysis the below code
//declaring the array
int[] a = { 1, -2, 4, 7, 23 };
//loop from k=1 to end of array
for (int k=1; k<a.length; k++)
//set current value equal to before array value
a[k]=a[k-1];
//loop and print array
for (int x: a)
System.out.println(x);
So when k = 1 , it will set a[1] = a[0] that is 1 hence a[1] will be 1
array will be 1,1,4,7,23
Similiarly when k=2 it will set a[2] = a[`] that is 1 hence a[1] will be 1
array will be 1,1,1,7,23
It will do the same till end of array
Finally value of array will be
1,1,1,1,1 and same will print in console
So output will be 1 in each line 5 times like below
1
1
1
1
1
Problem 8. Please provide the Rational class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.