/*1. convert the following while loop to for loop int count = 0; while (count <5
ID: 3774432 • Letter: #
Question
/*1. convert the following while loop to for loop
int count = 0;
while (count <50)
{
System.out.println("count is" + count);
count++;
}
*/
/* 2. convert the following for loop to a while loop
* for (int x = 50; x>0;x--)
* {
* System.out.println(x +"seconds to go.");
* }
3. Write an input validation loop that ask the ask the user to enter a number in the range of 1 through 4.
6. write the header of a method named mymethod that takes String and an integer as argument, and it returns a double
7. Given the method definition below write a statementt that calls the the method, passes the vale 4 to the method and assigns its returns value to a variable named result.
public static int cube (int num)
{
retun num *num*num
}
9. declare a one-dimensional array of strings and initialize the first element of the array with your first name, and element with your last name.
10. Declare a two dimensional array of double called myArray consisting of 3 rows and 2 columns
11. write a statemnet to assign the value 2.5 to the element of myArray two- dimensional array located in the third row and second column.
Explanation / Answer
1. convert the following while loop to for loop
int count = 0;
while (count <50)
{
System.out.println("count is" + count);
count++;
}
*/
Answer:
for(int count= 0; count<50;count++)
{
System.out.println("count is" + count);
}
2. convert the following for loop to a while loop
* for (int x = 50; x>0;x--)
* {
* System.out.println(x +"seconds to go.");
* }
Answer:
int x = 50;
while (x > 0)
{
System.out.println(x +"seconds to go.");
x--;
}
3. Write an input validation loop that ask the ask the user to enter a number in the range of 1 through 4.
Answer:
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number in the range of 1 through 4: ");
int number = sc.nextInt();
// Validating the input using loop.
while (number < 1 || number > 4)
{
System.out.println("Entered number is a invalid number.");
}
6. write the header of a method named mymethod that takes String and an integer as argument, and it returns a double
Answer: public static double mymethod(String str,int n);
7. Given the method definition below write a statementt that calls the the method, passes the vale 4 to the method and assigns its returns value to a variable named result.
public static int cube (int num)
{
retun num *num*num
}
Answer:
int result=cube(4);
9. declare a one-dimensional array of strings and initialize the first element of the array with your first name, and element with your last name.
Answer:
String[] array=new String[2];
array[0]="first name";
array[1]="second name";
10. Declare a two dimensional array of double called myArray consisting of 3 rows and 2 columns
Answer:
double[][] myArray=new double[3][2];
11. write a statemnet to assign the value 2.5 to the element of myArray two- dimensional array located in the third row and second column.
Answer:
myArray[2][1]=2.5;
Note:In case of any doubt please ask the question,Thanks.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.