This question has 4 parts. 1a-d. Again, it\'s one question with 4 parts. All par
ID: 3606189 • Letter: T
Question
This question has 4 parts. 1a-d. Again, it's one question with 4 parts. All parts need to be answered correctly. You must: Comment and Format and Must make sure about indenting. If you don't follow these steps, it will be flagged for review.
This is on Dialog Boxes, loops and Strings- So make sure to use these methods.
This is on Dialog Boxes, loops and Strings Exercise 1a. Show a program containing 2 for loops that prints the following lines using while loops then change to for loops 123456789 10 810 12 14 16 18 20 22 24 26 Exercise 1b. Write a Java program that uses three input dialog boxes to get three integer values from the user. The program then determines the sum and average of the three values and uses a message dialog box to show the results. Test the program with two sets of data and include the results in this report. Make sure to make your message/input dialogs neat and easy to read in this report (you will need to copy/paste them in your document) Sample output: Input Enter the first value: Enter the second value: Enter the third value The sum is 30 The averege is 10.0 Cancel Cancel Exercise 1c. Write a program containing 3 while loops that prints the following lines and show the output for each 9 8 7 6 5 4 3 21 0 1 3 5 7 9 11 13 15 17 19 0 2 4 6 8 10 12 14 16 18 Exercise 1d. Write a program containing 3 for loops that does the same as exercise 2. Show the output from each loopExplanation / Answer
Exercise1a
public class Looping1 {
public static void main(String[] args) {
int num=1;
//This while loop runs from 1 to 10 num=1
while(num<=10) {
System.out.print(num+" ");//Displaying the number
num++;//Increase num by 1
}
System.out.println();
//This for loop runs from 8 to 26 with increment of 2
for(int i=8;i<=26;i=i+2)
System.out.print(i+" ");
}
}
Exercise1b
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class SumAverage {
public static void main(String[] args) {
JFrame frame = new JFrame();
//Using inputDialog to input number 1
String num1 = JOptionPane.showInputDialog(
frame,
"Enter the first value:",
"Input",
JOptionPane.QUESTION_MESSAGE
);
//Using inputDialog to input number 2
String num2 = JOptionPane.showInputDialog(
frame,
"Enter the second value:",
"Input",
JOptionPane.QUESTION_MESSAGE
);
//Using inputDialog to input number 3
String num3 = JOptionPane.showInputDialog(
frame,
"Enter the third value:",
"Input",
JOptionPane.QUESTION_MESSAGE
);
//Converting strings to integers
int n1=Integer.parseInt(num1);
int n2=Integer.parseInt(num2);
int n3=Integer.parseInt(num3);
//Calculating sum
int sum=n1+n2+n3;
//Calculating average
double average = (double)sum/3;
//Output the sum and average on message dialog
JOptionPane.showMessageDialog(null, "The sum is "+sum+" The average is "+average,"Message", JOptionPane.INFORMATION_MESSAGE);
}
}
Exercise 1c
public class Looping2 {
public static void main(String[] args) {
int num=9;
//This while loop runs from 9 to 0 decrementing by 1
while(num>=0) {
System.out.print(num+" ");
num--;
}
System.out.println();
num=1;
//This while loop runs from 1 to 19 incrementing by 2
while(num<=19) {
System.out.print(num+" ");
num=num+2;
}
System.out.println();
num=0;
//This while loop runs from 0 to 18 incrementing by 2
while(num<=18) {
System.out.print(num+" ");
num=num+2;
}
}
}
Exercise 1d
public class Looping3 {
public static void main(String[] args) {
//This for loop runs from 9 to 0 decrementing by 1
for(int i=9;i>=0;i--)
System.out.print(i+" ");
System.out.println();
//This for loop runs from 1 to 19 incrementing by 2
for(int i=1;i<=19;i=i+2)
System.out.print(i+" ");
System.out.println();
//This for loop runs from 0 to 18 incrementing by 2
for(int i=0;i<=18;i=i+2)
System.out.print(i+" ");
System.out.println();
}
}
*************************************************************************************************************************
The sample input and output were as expected. As given in the question. I hope this helps you. All the best. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.