Skip to main content

ConsoleProgram

public class Area extends ConsoleProgram
{
    public void run()
    {
        double width = readDouble("What is the width of the rectangle?");
        double height = readDouble("What is the height of the rectangle?");
        double area = (width * height);
        System.out.println("The area of the rectangle is " + area + " units squared.");
    }
}

public class AddFractions extends ConsoleProgram
{
    public void run()
    {
        int numer_1 = readInt("What is the numerator of the first fraction? ");
        int denom_1 = readInt("What is the denominator of the first fraction? ");
        int numer_2 = readInt("What is the numerator of the second fraction? ");
        int denom_2 = readInt("What is the denominator of the second fraction? ");
        int sum_1 = ((numer_1 * denom_2) + (denom_1 * numer_2));
        int sum_2 = (denom_2 * denom_1);
        System.out.println("The sum of " + numer_1 + "/" + denom_1 + " + " + numer_2 + "/" + denom_2 + " = " + sum_1 + "/" + sum_2);
    }
}

public class CastingToInt extends ConsoleProgram
{
    public void run()
    {
        double myDouble = readDouble("Please input your double: ");
        int myInt = (int)myDouble;
        System.out.println(myInt);
    }
}

public class CastingToDouble extends ConsoleProgram
{
    public void run()
    {
        int firstInt = readInt("First Int: ");
        int secondInt = readInt("Second Int: ");
        double dub_1 = (double)firstInt;
        double dub_2 = (double)secondInt;
        System.out.println(dub_1 / dub_2);
    }
}

public class MovieRatings extends ConsoleProgram
{
    public void run()
    {
        double movieRating = readDouble("Please enter a movie rating: ");
        movieRating = movieRating + .5;
        int movieRounded = (int) movieRating;
        System.out.println("Rating rounded: " + movieRounded);
       
    }
}

public class IceCream extends ConsoleProgram
{
    public void run()
    {
        boolean likeCream = readBoolean("Do you like icecream? ");
                System.out.println(likeCream);

    }
}

public class RollerCoaster extends ConsoleProgram
{
    public void run()
    {
        boolean isTallEnough = readBoolean("Taller than 4ft? ");
        boolean isOldEnough = readBoolean("Older than 8? ");
        boolean canRide = isTallEnough && isOldEnough;
        System.out.println(canRide);
    }
}

public class TripleDouble extends ConsoleProgram
{
    public void run()
    {
        int points = readInt("How many points did you score? ");
        int rebounds = readInt("How many rebounds did you get? ");
        int assists = readInt("How many assists did you have? ");
       
        // Create a boolean called `tripleDouble`
        // that is true if
        // you had at least 10 in each category
        boolean tripleDouble = points >= 10 && rebounds >= 10 && assists >= 10;
       
        System.out.println("Got a Triple Double?: " + tripleDouble);
    }
}

public class Repeat1000 extends ConsoleProgram
{
    public void run()
    {
        for(int i = 0; i < 1000; i++)
            {
                System.out.println("Hello Karel");
            }
    }
}

public class Odds extends ConsoleProgram
{
    public void run()
    {
        for(int i = 1; i < 100; i+=2)
            {
                System.out.println(i);
            }
    }
}

public class Factorial extends ConsoleProgram
{
    public void run()
    {
        int num = readInt("What number would you like to compute the factorial for?");
        int fact = 1;
        for(int i = 1; i <= num; i++)
            {
                fact *= i;
            }
        System.out.println(fact);
    }
}


public class EvensWhile extends ConsoleProgram
{
    public void run()
    {
        int i = 0;
        while (i <= 500)
            {
                System.out.println(i);
                i += 2;
            }
    }
}

public class FindMinimum extends ConsoleProgram
{
    public void run()
    {
        int first = readInt("First number? ");
        int second = readInt("Second number? ");
        int third = readInt("Third number? ");
        int lowest = 0;
        if(first < second)
            {
                lowest = first;
            }
        else
            {
                lowest = second;
            }
        if(second < third)
            {
                lowest = second;
            }
        else
            {
                lowest = third;
            }
        if(first < third)
            {
                lowest = first;
            }
        else
            {
                lowest = third;
            }
        System.out.println(lowest);
    }
}

public class GuessTheNumber extends ConsoleProgram
{
    public void run()
    {
        int secretNumber = 6;
        System.out.println("I'm thinking of a number between 1 and 10.");
        System.out.println("See if you can guess the number!");
        while(true)
            {
                int guess = readInt("Enter your guess: ");
                if(guess == secretNumber)
                    {
                          System.out.println("Correct!");
                        break;
                    }
                System.out.println("Try again!");
            }
    }
}

public class Divisibility extends ConsoleProgram
{
    public void run()
    {
        int dividend = readInt("Enter the dividend: ");
        int divisor = readInt("Enter the divisor: ");
       
       
        if(divisor != 0 && dividend / divisor == (double) dividend / divisor)
        {
            System.out.println(dividend + " is divisible by " + divisor + "!");
        }
        else
        {
            System.out.println(dividend + " is not divisible by " + divisor);
        }
    }
}

public class AmusementPark extends ConsoleProgram
{
    int AGE_LIMIT = 12;
    int HEIGHT_LIMIT = 48;
   
    public void run()
    {
        int age = readInt("Enter your age: ");
        int height = readInt("Enter your height in inches: ");
       
        boolean oldEnough = age >= AGE_LIMIT;
        boolean tallEnough = height >= HEIGHT_LIMIT;
       
        // CHANGE THIS LINE
        // Convert this boolean expression into its De Morgan equivalent
        boolean cannotRide = !oldEnough || !tallEnough;
       
        if(cannotRide)
        {
            System.out.println("You may not ride the rollercoasters.");
        }
        else
        {
            System.out.println("You may ride the rollercoasters!");
        }
       
        boolean canSwim = readBoolean("Can you swim? ");
        boolean hasLifeJacket = readBoolean("Do you have a life jacket? ");
       
        // CHANGE THIS LINE
        // Convert this boolean expression into its De Morgan equivalent
        boolean cannotSwim = !canSwim && !hasLifeJacket;
       
        if(cannotSwim)
        {
            System.out.println("You may not swim in the pool.");
        }
        else
        {
            System.out.println("You may swim in the pool!");
        }
    }
}

public class ThreeStrings extends ConsoleProgram
{
    public void run()
    {
        // Ask the user for three strings.
        // remember to use the readLine() method.
        String First = readLine("First string? ");
        String Second = readLine("Second string? ");
        String Third = readLine("Third string? ");
        String combine = First + Second;
        if(combine.equals(Third))
            {
                System.out.println(First + " + " + Second + " is equal to " + Third + "!");
            }
        else
            {
                System.out.println(First + " + " + Second + " is not equal to " + Third + "!");
            }
    }
}


**TEST**
Imgur Link to album : https://imgur.com/a/Ohla7