Department of Computer Science
Faculty of Physical Sciences
Ahmadu Bello University, Zaria

COSC 211 : Object Oriented Programming I - LAB03


To gain experience with:


1.  Reading Input

There are various ways to read input from the keyboard, the java.util.Scanner class is one of them.

The Java Scanner class breaks the input into tokens using a delimiter that is whitespace bydefault. It provides many methods to read and parse various primitive values.

Java Scanner class is widely used to parse text for string and primitive types using regular expression.

There is a list of commonly used Scanner class methods:

public String next()it returns the next token from the scanner.
public String nextLine() it moves the scanner position to the next line and returns the value as a string.
public byte nextByte() it scans the next token as a byte.
public short nextShort() it scans the next token as a short value.
public int nextInt() it scans the next token as an int value.
public long nextLong() it scans the next token as a long value.
public float nextFloat() it scans the next token as a float value.
public double nextDouble() it scans the next token as a double value.


Example 1: Study, compile, run and observe the output of the following program

import java.util.Scanner;

public class ReadingInput {
  public static void main(String[] args)throws IOException {
    Scanner input = new Scanner(;
    System.out.print("Please enter your name: ");
    String name = input.nextLine();
    System.out.print("Now enter your age: ");
    int age = input.nextInt();
	System.out.print("Enter amount to donate: ");
	double amount = input.nextDouble();
    System.out.printf("Mr. %s after one year you will be %d years\n",name, (age+1));
	System.out.printf("This is the amount you donated $%,.2f",amount);


3.  Assignments

1.        Write a Java program that prompts for and reads the length, width, and height (in centimeters) of a closed box. The program should then compute and display

(a)     the volume of the box.

(b)     the surface area of the box.

2.        Write a program that prompts for and read e-mail address a user.  The program them prints the user name and the domain name on different lines.(Hint: use indexOf() and substring() method)


Write a program that prompts user to read the sides and compute the area, perimter, longest, and smallest side of triangle. Compute and display the remainder when the longest side is divided by the smallest side. Print and format the output as

The perimter of triangle is 11.00
The area of triangle with sides a=6, b=7, and c=9 is 25.69unit square
The longest side is 9
The smallest side is 6
The remainder is 3