0% found this document useful (0 votes)
51 views8 pages

OOP-Lab Week3+4 With Solution - Tagged

This document contains 6 programming exercises with solutions in Java. The exercises include writing programs to print a lucky number, name and message with formatting, a multiplication table, read user input and print a welcome message, convert seconds to hours/minutes/seconds, and calculate the minimum number of bills needed to equal a given amount of Saudi Riyals. The solutions demonstrate basics of Java programming including printing, user input, calculations, and conditional logic.

Uploaded by

Sara Moh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views8 pages

OOP-Lab Week3+4 With Solution - Tagged

This document contains 6 programming exercises with solutions in Java. The exercises include writing programs to print a lucky number, name and message with formatting, a multiplication table, read user input and print a welcome message, convert seconds to hours/minutes/seconds, and calculate the minimum number of bills needed to equal a given amount of Saudi Riyals. The solutions demonstrate basics of Java programming including printing, user input, calculations, and conditional logic.

Uploaded by

Sara Moh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
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 3 – Week 4
Exercise 1: Lucky number
Write a program that print your lucky number. Name your class LuckyNumber. The
output must be as the following:

Solution:
public class LuckyNumber{
public static void main (String []args) {
System.out.print("My lucky number is 3");
} //end of main
} // end of class

Exercise 2: print information


Write a Java program that prints the following:

Note 1: Change the output so it prints your name in the third line.
Note 2: use \n and \t

Page | 3
Solution:
public class Java1
{
public static void main(String [] args)
{
System.out.println();
System.out.println("Hello,");
System.out.println(" =============================");
System.out.println(" My name is Khalid"); //name changed
System.out.println(" This is my first Java program");
System.out.println(" =============================");
System.out.println("Thank you");
System.out.println("+---+---+");
System.out.println("| O | |");
System.out.println("+---+---+---+");
System.out.println(" | X | |");
System.out.println(" +---+---+");
System.out.println();
}}

Exercise 3: multiplication Table


Write a Java program that prints the multiplication table of 9 as the following:

Solution:
public class MultTab
{
public static void main (String [] arg)
{
System.out.println("Multuplication table of 9 (1-5)");
System.out.println("1 * 9 = " + 1*9);
System.out.println("2 * 9 = " + 2*9);
System.out.println("3 * 9 = " + 3*9);
System.out.println("4 * 9 = " + 4*9);
System.out.println("5 * 9 = " + 5*9);
}
}

Page | 4
Exercise 4: Hello program!!!

Scanner class in java library is used to read keyboard input.


Write a program that do the following:
- Ask the user to enter his/her full name.
- Read the input and store it in the variable (name)
- Print a welcome message with the user name.
Solution:
import java.util.Scanner;
public class Midterm{
public static void main (String[] args){
//Ask the user to enter his/her full name.
System.out.println("Enter your full name");
//Read the input and store it in the variable (name)
Scanner in = new Scanner (System.in);
String name= in.nextLine();
//Print a welcome message with the user name.
System.out.println("Welcome, " + name);
}}

Exercise 5: Seconds converter


Write java program that allow the user to enter:
- The number of seconds
- The number of minutes
- The number of hours
Print the corresponding total number of seconds
Typical run of the program

Page | 5
Solution:

import java.util.Scanner;
public class SecondsConverter{
public static void main (String [] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of seconds: ");
int s = in.nextInt();

System.out.println("Enter the number of minutes: ");


int m = in.nextInt();

System.out.println("Enter the number of hours: ");


int h = in.nextInt();

int totalSeconds = s + m*60 + h*3600;


System.out.println(s + " seconds + " + m + " minutes + " + h +
" hours = " + totalSeconds + " seconds" );
System.out.println("End of the program");
}}

Exercise 6: SAR bills calculator


Create java program that ask the user to enter an amount in Saudi Riyal.
The program must print the minimum number of bills required to get the
given amount.
The program should also print the number within each kind of bill.
Typical run of the program:
Scenario 1:

Page | 6
Scenario 2:

Solution:
import java.util.Scanner;
public class SarBillsCalculator
{
public static void main(String [] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Give an amount: Saudi Riyal: ");
int x = in.nextInt();
int amount=x;

int nb_500 = amount/500;


amount = amount -nb_500 * 500;

int nb_100 = amount/100;


amount = amount -nb_100 * 100;

int nb_50 = amount/50;


amount = amount -nb_50 * 50;

int nb_10 = amount/10;


amount = amount -nb_10 * 10;

int nb_5 = amount/5;


amount = amount -nb_5 * 5;

int nb_1 = amount;

Page | 7
int totPaper =nb_500+nb_100+nb_50+nb_10+nb_5+nb_1;
System.out.println("The minimum number of bills required for the
amount" + x + " is: " + totPaper);
System.out.println("500 SR: "+nb_500);
System.out.println("100 SR: "+nb_100);
System.out.println("50 SR: "+nb_50);
System.out.println("10 SR: "+nb_10);
System.out.println("5 SR: "+nb_5);
System.out.println("1 SR: "+nb_1);
}
}

Page | 8

You might also like