Question
1. (20 points) Write a Java program that asks the user to input a positive integer n first, then create an array of size n.
1. (20 points)
Write a Java program that asks the user to input a positive integer n first, then create an array of size n. Fill n random integers between 5 and 555, inclusively, into the created array. Output the sum of all the integers in the array and the average of all the integers in the array.
2. (10 points)
Find the output of the following Java program and explain your answer.
public class NO2HW1
{
public static void main(String args[])
{
int min = 100;
int a = 10;
int b = 20;
min_method(a, b, min);
System.out.println(min);
}
public static void min_method(int n1, int n2, int min)
{
if (n1 < n2)
min = n1;
else
min = n2;
System.out.println(min);
}
}
3. (10 points)
Find the output of the following Java program and explain your answer.
class Circle
{
double radius;
Circle(double r) { radius = r; }
}
public class NO3HW1
{
public static void main(String args[])
{
Circle circle1 = new Circle(10.0);
Circle circle2 = new Circle(20.0);
System.out.println("Before invoking swap: " +
" the radius of circle1 is " + circle1.radius +
" the radius of circle2 is " + circle2.radius);
swap(circle1, circle2);
System.out.println(" After invoking swap: " +
" the radius of circle1 is " + circle1.radius +
" the radius of circle2 is " + circle2.radius);
}
public static void swap(Circle x, Circle y)
{
System.out.println(" Before swap: " +
" the radius of circle x is " + x.radius +
" the radius of circle y is " + y.radius);
Circle temp = x;
x = y;
y = temp;
System.out.println(" After swap: " +
" the radius of circle x is " + x.radius +
" the radius of circle y is " + y.radius);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started