OOP-Lab Week5 With Solution - Tagged
OOP-Lab Week5 With Solution - Tagged
Solution:
import java.util.Scanner;
public class Ex1 {
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
System.out.println("Please Enter a Number");
int n = in.nextInt();
if (n>0)
System.out.println("This is a positive number");
else
System.out.println("this a negative number");
if(n%2 ==1)
System.out.println("This is an Odd number");
else
System.out.println("this is an even number");
}
}
Exercise 2:
Take three numbers from the user and print the greatest number and the
smaller number.
Solution:
import java.util.Scanner;
{
Scanner input = new Scanner(System.in);
Page | 3
System.out.print("Kindely enter three numbers (click enter after each
number):");
}
}
Exercise 3:
Write a Java program that reads two floating-point numbers and tests
whether they are the same up to three decimal places.
Solution:
//This Mohammed Saffar Code
import java.util.Scanner;
public class Ex3
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Kindely enter two numbers (click enter after
each number):");
Page | 4
double num1 = input.nextDouble();
double num2 = input.nextDouble();
Exercise 4:
Write a Java program that reads a floating-point number and prints "zero" if
the number is zero. Otherwise, print "positive" or "negative". Add "small" if
the absolute value of the number is less than 1, or "large" if it exceeds
1,000,000.
Solution:
import java.util.Scanner;
public class Exercise4 {
if (input > 0)
{
if (input < 1)
Page | 5
{
System.out.println("Positive small number");
}
else if (input > 1000000)
{
System.out.println("Positive large number");
}
else
{
System.out.println("Positive number");
}
}
else if (input < 0)
{
if (Math.abs(input) < 1)
{
System.out.println("Negative small number");
}
else if (Math.abs(input) > 1000000)
{
System.out.println("Negative large number");
}
else
{
System.out.println("Negative number");
}
}
else
{
System.out.println("Zero");
}
}
}
Page | 6
Exercise 5:
Write a Java program to solve quadratic equation: ax2 + bx + c =0
Solution:
import java.util.Scanner;
public class Ex5 {
public static void main(String[] Strings) {
System.out.print("Input a: ");
double a = input.nextDouble();
System.out.print("Input b: ");
double b = input.nextDouble();
System.out.print("Input c: ");
double c = input.nextDouble();
}
}
Exercise 6 (Extra):
Write a Java program that asks the user to provide a single character from
the alphabet. Print Vowel or Consonant, depending on the user input. If the
user input is not a letter (between a and z or A and Z), or is a string of length
> 1, print an error message.
Page | 7
import java.util.Scanner;
public class Ex6 {
if (input.length() > 1)
{
System.out.println("Error. Not a single character.");
}
else if (!(uppercase || lowercase))
{
System.out.println("Error. Not a letter. Enter uppercase or
lowercase letter.");
}
else if (vowels)
{
System.out.println("Input letter is Vowel");
}
else
{
System.out.println("Input letter is Consonant");
}
}
}
Page | 8