Object-Oriented Programming with Java, part I – Exercise 9

Exercise 9

This program asks the user for two integers and prints their quotient making sure that 3/2=1.5.

My code

import java.util.Scanner;

public class Divider {

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);

        // Implement your program here. Remember to ask the input from user.
        System.out.println("Type a number: ");
        double firstNumber = Integer.parseInt(reader.nextLine());
        
        System.out.println("Type another number: ");
        double secondNumber = Integer.parseInt(reader.nextLine());
        
        System.out.println("Division: " + firstNumber + "/" + secondNumber + " = " +(firstNumber / secondNumber));
        
    }
}

Model code

import java.util.Scanner;

 

public class Divider {

 

    public static void main(String[] args) {

        Scanner reader = new Scanner(System.in);

 

        // Implement your program here. Remember to ask the input from user.

        System.out.print("Type a number: ");

        int firstNumber = Integer.parseInt(reader.nextLine());

        System.out.print("Type another number: ");

        int secondNumber = Integer.parseInt(reader.nextLine());

 

        System.out.println(""); // an empty line

        System.out.println("Division: " + firstNumber + " / " + secondNumber +

                           " = " + (1.0 * firstNumber / secondNumber));

    }

}

 
Leave a Reply

Your email address will not be published. Required fields are marked *