PF1 Lab4 Write a Java program that will perform the steps described below. Your
ID: 3724710 • Letter: P
Question
PF1 Lab4 Write a Java program that will perform the steps described below. Your program should perform these steps in the order specified. Put your lab in the file ~/PF1/lab4/Lab4.java In each case, if you are asked to display an answer/value, do it in a complete sentence. For instance to display the number of states in the US, you would display: The number of states in the US is 50 (or something similar) If you are not asked to display the answer, that step does not need to output anything. Assume the questions are cumulative. IE any change made in one step will affect the following steps. If you want/need to declare/use variables that I did not mention, do so. Please add comments, etc to your code so I can tell what problem number each line (or section) of code solves. You should write, debug and compile this program, leaving the ".class" file in your directory so that I can run your program to verify it. I'll grade online and leave comments/grade in a file named README.MCU ========================================================================= 1) Using a for loop, display the numbers from 1 to 10, one per line. 2) Using a for loop, display the numbers from 10 to 1, one per line, with each number followed by an exclamation point. 3) For your nerdy amusement, display the the string "Blastoff!!!". =) 4) Use an array initializer to declare and initialize an array called "numbers" that initially contains the values 11, 8, -7, 22, 3, -17, 6, 25, 10, 5, -12, -9, 14, 20, -13 5) Display (one per line) the elements of the array "numbers". 6) Using a for loop, display the numbers from 0 to 10, counting by two. 7) Display the elements in the "numbers" array that are even numbers. 8) Display the elements in the "numbers" array that reside at even indexes. 9) Display the elements in the "numbers" array that are negative. 10) Display the elements in the "numbers" array that are multiples of 5. 11) Calculate and display the sum of all of the values in the numbers array. 12) Find the largest element in the "numbers" array. Save the largest value in a variable named "largestNum". Save the index of the largest value in "indexLargestNum" 13) Display "largestNum" and "indexLargestNum". 14) For each element in "numbers", display whether the element is a) less than -15 b) between -15 and 15 c) greater than 15 15) Rewrite the following piece of code using a "while loop": for (i=50; i>25; i-=5) System.out.println("i = " + i);
Explanation / Answer
Program:
public class klsnczxcvs {
public static void main(String[] args) {
//task1
for(int i=1;i<=10;i++)
System.out.println(i);
//task2
for(int i=10;i>=1;i--)
System.out.println(i+"!");
//task3
System.out.println("Blastoff!!!");
//task4
int numbers[]={ 11, 8, -7, 22, 3, -17, 6, 25, 10, 5, -12, -9, 14, 20, -13};
//task5
for(int i=0;i<numbers.length;i++)
System.out.println(numbers[i]);
//task6
for(int i=0;i<=10;i=i+2)
System.out.println(i);
//task7
System.out.println("Even numbers in numbers array");
for(int i=0;i<numbers.length;i++)
{
if(numbers[i]%2==0)
System.out.println(numbers[i]);
}
//task8
System.out.println("Elements at even indexes in numbers array");
for(int i=0;i<numbers.length;i=i+2)
{
System.out.println(numbers[i]);
}
//task9
System.out.println("Negeative numbers in numbers array");
for(int i=0;i<numbers.length;i++)
{
if(numbers[i]<0)
System.out.println(numbers[i]);
}
//task10
System.out.println("Multiples of 5 in numbers array");
for(int i=0;i<numbers.length;i++)
{
if(5%numbers[i]==0)
System.out.println(numbers[i]);
}
//task11
int sum=0;
for(int i=0;i<numbers.length;i++)
{
sum+=numbers[i];
}
System.out.println("Sum of elements in numbers array is "+sum);
//task12
int largestNum=numbers[0];
int indexLargestSum=0;
for(int i=0;i<numbers.length;i++)
{
if(numbers[i]>largestNum)
{
largestNum=numbers[i];
indexLargestSum=i;
}
}
//task13
System.out.println("Largest number in numbers array is "+largestNum+" at position "+indexLargestSum);
//task14
for(int i=0;i<numbers.length;i++)
{
if(numbers[i]<-15)
{
System.out.println(numbers[i]+" is lessthan -15");
}
else if(numbers[i]>-15 && numbers[i]<15)
System.out.println(numbers[i]+" is between -15 and 15");
else
System.out.println(numbers[i]+" is greater 15");
}
//task 15
int i=50;
while(i>25)
{
System.out.println("i="+i);
i-=5;
}
}
}
Output:
1
2
3
4
5
6
7
8
9
10
10!
9!
8!
7!
6!
5!
4!
3!
2!
1!
Blastoff!!!
11
8
-7
22
3
-17
6
25
10
5
-12
-9
14
20
-13
0
2
4
6
8
10
Even numbers in numbers array
8
22
6
10
-12
14
20
Elements at even indexes in numbers array
11
-7
3
6
10
-12
14
-13
Negeative numbers in numbers array
-7
-17
-12
-9
-13
Multiples of 5 in numbers array
5
Sum of elements in numbers array is 66
Largest number in numbers array is 25 at position 7
11 is between -15 and 15
8 is between -15 and 15
-7 is between -15 and 15
22 is greater 15
3 is between -15 and 15
-17 is lessthan -15
6 is between -15 and 15
25 is greater 15
10 is between -15 and 15
5 is between -15 and 15
-12 is between -15 and 15
-9 is between -15 and 15
14 is between -15 and 15
20 is greater 15
-13 is between -15 and 15
i=50
i=45
i=40
i=35
i=30
output is too long unable to take screen shot.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.