Object-Oriented Programming with Java, part II – Exercise 3

Let’s create an interactive program to control two liquid containers. The containers are named “first” and “second”. Each are capable of containing 100 liters of liquid at a time.

My Code

LiquidContainers


import java.util.Scanner;

public class LiquidContainers {

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

        int firstCont = 0;
        int secondCont = 0;

        while (true) {
            //print results
            System.out.println("First: " + firstCont + "/100");
            System.out.println("Second: " + secondCont + "/100");

            String input = scan.nextLine();
            if (input.equals("quit")) {
                break;
            }

            String[] parts = input.split(" ");
            String command = parts[0];
            int amount = Integer.valueOf(parts[1]);

            if (amount > 0) {
                //add command
                if (command.contentEquals("add")) {
                    firstCont += amount;
                    if (firstCont > 100) {
                        firstCont = 100;
                    }
                }
                //move command for firstCont
                if (command.contentEquals("move")) {
                    if (firstCont >= amount) {
                        firstCont -= amount;
                        secondCont += amount;
                    } else if (firstCont < amount){
                        secondCont+= firstCont;
                        firstCont = 0;
                        
                    }
                    if (secondCont > 100){
                        secondCont = 100;
                    }
                }
                //remove command for secondCont
                if (command.contentEquals("remove")) {
                    if (secondCont >= amount){
                        secondCont -= amount;
                        
                    } else if (secondCont < amount){
                        secondCont = 0;
                    }
                }
            }
            

        }
    }

}

Model Code

LiquidContainers

import java.util.Scanner;

 

public class LiquidContainers {

 

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

 

        int first = 0;

        int second = 0;

 

        while (true) {

            System.out.println("First: " + first + "/100");

            System.out.println("Second: " + second + "/100");

            System.out.print("> ");

 

            String input = scan.nextLine();

            if (input.equals("quit")) {

                break;

            }

 

            String[] partsOfInput = input.split(" ");

            input = partsOfInput[0];

            int amount = Integer.valueOf(partsOfInput[1]);

 

            if (input.equals("add") && amount > 0) {

                first = first + amount;

                if (first > 100) {

                    first = 100;

                }

            }

 

            if (input.equals("move") && amount > 0) {

                if (amount > first) {

                    amount = first;

                }

 

                first = first - amount;

                second = second + amount;

 

                if (second > 100) {

                    second = 100;

                }

            }

 

            if (input.equals("remove") && amount > 0) {

                second = second - amount;

                if (second < 0) {

                    second = 0;

                }

            }

 

            System.out.println("");

        }

    }

 

}

Comments

OK, their way was slightly simpler but I wasn’t that far off. I seem to follow the instructions rather step by step and literally and that means I am not stepping back and seeing the bigger picture.

Leave a comment