1. Write Java statements to accomplish each of the following tasks a. Declare va
ID: 3600129 • Letter: 1
Question
1. Write Java statements to accomplish each of the following tasks a. Declare variable sum of type int and initialize it with 0 b. Print passed' if a grade variable is greater than or equal to 50, and failed' if the grade is les than 50 c. Add variable x to variable sum, and assign the result to variable sum 2. What does the following statement do? (Explain in English) System.out.println(x21 "Odd" "Even" 3. Develop a Java application that calculates and prints the sum of the integers from to 10. Use a while statement to loop through the calculation and increment statements. 4. What is the result of executing this program? public class Mystery2 public static void main( String[] args ) int count = 1; while countExplanation / Answer
1)
public class Prog1 {
public static void main(String[] args) {
int sum=0;//int var sum initialize to 0
if(sum>=50)//if greater or equal to 50
System.out.println("Passed");
else
System.out.println("failed");//if less than 50 failed
int x=23;//var x
sum=sum+x;//adding to sum
}
}
//OUTPUT : failed
2)
System.out.println(x%2==1?"odd":"even");
above statement is boolean expresion that prints even or odd
x%2==1 this means if x divided by and if remainder is one then odd will execute like x = 7
if x%2==1 is false then even will be printed like x = 4
3)
public class Prog2 {
public static void main(String[] args) {
int sum=0;//Initialize sum to 0
int count=0;//increment value initialize to 0
while(count<=10) {//using while
sum+=count;//adding the sum
count++;//incrementing the counter
}
System.out.println(sum);//printing the sum
}
}
//OUTPUT : 55
4)
public class Mystery2 {
public static void main(String[] args) {
int sum=0;//Initialize sum to 0
int count=1;//increment value initialize to 0
while(count<=10) {
System.out.println(count % 2==1?"****":"++++++++");
count=count+1;
}//end while
}//end main
}//end class
logic: if count is odd then it will print **** and if count is even it will print +++++++
//OUTPUT :
****
++++++++
****
++++++++
****
++++++++
****
++++++++
****
++++++++
5)
Pseudocode:
Counter=0;
Max=0;
While Counter <10:
number = getValue();
if Max < number
Max = number
Counter = Counter+1
END
//java program
public class Prog3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int counter=0;//initialize counter to 0
int largest=0;//initialize largest to 0
int number ;//number var
while(counter<10)//using while loop for
{
System.out.print("Enter the value : ");
number= sc.nextInt();
if (largest<number)//checking for max
largest=number;
counter++;//incrementing the counter var
}
System.out.println("Largest : "+largest);
sc.close();
}
}
//OUTPUT
Enter the value : 34
Enter the value : 654
Enter the value : 23
Enter the value : 888
Enter the value : 964
Enter the value : 455
Enter the value : 345
Enter the value : 234
Enter the value : 543
Enter the value : 445
Largest : 964
//Please do let me know if u have any concern...
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.