0% found this document useful (0 votes)
60 views

OOP-Lab Week5 With Solution - Tagged

This document contains an instructor lab manual for an object oriented programming course. It includes 6 programming exercises with solutions in Java. The exercises cover topics like getting user input, comparing numbers, solving quadratic equations, and identifying vowels/consonants. The document provides the code solutions to each exercise on the corresponding page.

Uploaded by

Sara Moh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

OOP-Lab Week5 With Solution - Tagged

This document contains an instructor lab manual for an object oriented programming course. It includes 6 programming exercises with solutions in Java. The exercises cover topics like getting user input, comparing numbers, solving quadratic equations, and identifying vowels/consonants. The document provides the code solutions to each exercise on the corresponding page.

Uploaded by

Sara Moh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Instructor Lab Manual

OOP: Object Oriented


Programming

CS230 - IT232 - CS140

College of Computing & Informatics

SAUDI ELECTRONIC UNIVERSITY


Page | 1
Page | 2
Week 5
Exercise 1:
Write a Java program to get a number from the user and print whether it is
positive or negative and if it is odd or even.

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;

public class Ex2 {


public static void main(String[] args)

{
Scanner input = new Scanner(System.in);

Page | 3
System.out.print("Kindely enter three numbers (click enter after each
number):");

int num1 = input.nextInt();


int num2 = input. nextInt ();
int num3 = input. nextInt ();

if (num1 >= num3 && num1 >= num2)


System.out.println("The greatest number is = " + num1);
else if (num2 >= num1 && num2 >= num3)
System.out.println("The greatest number is = " + num2);
else
System.out.println("The greatest number is = " + num3);

if (num1 <= num3 && num1 <= num2)


System.out.println("The smallest number is = " + num1);
else if (num2 <= num1 && num2 <= num3)
System.out.println("The smallest number is = " + num2);
else
System.out.println("The smallest number is = " + num3);

}
}

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();

final double PL=1E-3;//=0.001

if (Math.abs(num1 - num2) <= PL)


System.out.println("Entered numbers are the same up to third
decimal place.");
else
{
if((num1 - num2)>0)
System.out.println("Greatest Number = " + num1);
else
System.out.println("Greatest Number = " + num2);
}
}

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 {

public static void main(String[] args)


{
Scanner in = new Scanner(System.in);
System.out.print("Input value: ");
double input = in.nextDouble();

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) {

Scanner input = new Scanner(System.in);

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();

double result = b * b - 4.0 * a * c;


if (result > 0.0) {
double r1 = (-b + Math.pow(result, 0.5)) / (2.0 * a);
double r2 = (-b - Math.pow(result, 0.5)) / (2.0 * a);
System.out.println("The roots are " + r1 + " and " + r2);
} else if (result == 0.0) {
double r1 = -b / (2.0 * a);
System.out.println("The root is " + r1);
} else {
System.out.println("The equation has no real roots.");
}

}
}

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 {

public static void main(String[] args)


{
Scanner in = new Scanner(System.in);

System.out.print("Input an alphabet: ");


String input = in.next().toLowerCase();

boolean uppercase = input.charAt(0) >= 65 && input.charAt(0) <=


90;
boolean lowercase = input.charAt(0) >= 97 && input.charAt(0) <=
122;
boolean vowels = input.equals("a") || input.equals("e") ||
input.equals("i")
|| input.equals("o") || input.equals("u");

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

You might also like