Lab Questions: (open new Project and name it Lab3 yourName) Q1-( create new main
ID: 3725412 • Letter: L
Question
Lab Questions: (open new Project and name it Lab3 yourName) Q1-( create new main class and name it Lab3Q1 and use it to solve this question) Write a java application that reads three integer numbers from the user, then calculate the sum of the first two numbers and print True" if the sum is divided by the third number or equal to the third number For example output: (note these input and outputs are only for example) Input the first number:7J Input the second number: 8J Input the third number:5J The result is: True Input the first number:9J Input the second number: 1J Input the third number 10J The result is: True Input the first number : 22 Input the second number:5J Input the third number:7 The result is: False 02- (Create new main class and name it Lab3Q2): Write a java program that reads 5 integer numbers from the user, then print all five numbers irn ascending order, for exampte output: (note these input and outputs are only for example) Enter number 1:4 Enter number 2:78 Enter number 3:2 Enter number 4:9 Enter number 5 :23J All numbers in ascending order: 249, 23·78 Submission instruction: 1. Use UQU eLearn portal to submit your Lab any email or hard copy won't be accepted 2. Deadline submission is at the end of lab session (Any LATE submission won't accepted) 3. Required files: ( ) your project including the twe main classes for Q1 and Q2 after exporting it to zip file Click on your project Click File Exporting Project To ZIP Click on Browse and then choose the place Desktop and write the file name ( yourProjectNamezip ) Click on ok Click on Export a. b. Make sure that the exported file name end with zip Please Note yon are fully responsible of your submitted files and you have to make sure it is the correct one before and after submission and you have to follow all the instruction mentioned earlier, 4. 5. Marking: (for each question) a. b. c. d. ½ mark for correct variable decleration ½ mark for correct arithmetic operations for comments ½ or correct run and outputExplanation / Answer
Q1)
import java.io.*;
import java.util.Scanner;
class Lab3Q1 // Create Main Class
{
public static void main(String args[])
{
Scanner scr=new Scanner(System.in); // Scanner Object scr
int first,second,third;
System.out.print("Input the First Number: ");
first=scr.nextInt(); // Read First Number using Scanner Object
System.out.print("Input the Second Number: ");
second=scr.nextInt(); // Read Second Number using Scanner Object
System.out.print("Input the Third Number: ");
third=scr.nextInt(); // Read Third Number using Scanner Object
int sum=first+second; // Calculate Sum of First and Second Numbers
if(sum==third || sum%third==0) // Check Condition
System.out.print("The Result is: True"); // condition is true print this
else
System.out.print("The Result is: False"); // condition is false print this
}
}
OUTPUT
F:>javac Lab3Q1.java
F:>java Lab3Q1
Input the First Number: 7
Input the Second Number: 8
Input the Third Number: 5
The Result is: True
F:>java Lab3Q1
Input the First Number: 9
Input the Second Number: 1
Input the Third Number: 10
The Result is: True
F:>java Lab3Q1
Input the First Number: 22
Input the Second Number: 5
Input the Third Number: 7
The Result is: False
Q2)
import java.io.*;
import java.util.Scanner;
class Lab3Q2 // Create Main Class
{
public static void main(String args[])
{
Scanner scr=new Scanner(System.in); // Create Scanner Object for reading
int num,temp; // Declare integer variables
int arr[]=new int[5]; // Declare integer array of size 5
int k=0; // Declare integer variable and initialize 0
for(int i=1;i<=5;i++) // Create for loop until 5 elements
{
System.out.print("Enter Number "+i+":");
num=scr.nextInt(); // Read 5 integer elements
arr[k++]=num; // store 5 integer numbers into array "a"
}
// Implementing Bubble sort technique
for (int i = 0; i < 5; i++)
{
for (int j = i + 1; j < 5; j++)
{
if (arr[i] > arr[j])
{
// swaping of elements
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
// print sorted elements with commas
System.out.print("All numbers in Ascending Order: ");
for (int i = 0; i < 5 ; i++)
{
System.out.print(arr[i] + ",");
}
}
}
OUTPUT
F:>javac Lab3Q2.java
F:>java Lab3Q2
Enter Number 1:4
Enter Number 2:78
Enter Number 3:2
Enter Number 4:9
Enter Number 5:23
All numbers in Ascending Order: 2,4,9,23,78,
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.