Graderpractice for programmers

254275 OOP - Labs 1-5 Review

Awaiting submission
Venus takes 224.70 Earth days to complete one orbit around the Sun (compared to Earth which takes 365.25 days). The program should input your age on Earth (integer) and output your age on Venus to 2 decimal places.
Awaiting submission
Write a program that takes a planet name and outputs information about that planet. The program should accept input in any case (e.g. “Mars” == “mars” == “MARS”).
Awaiting submission
Write a program that prints out multiples of 7. The number of multiples to print out is the input to the program.
Awaiting submission
Write a program that inputs 4 numbers and returns the smallest.
Awaiting submission
Write a program that inputs a number x and generates the sum of x + (x-1) + … + 1. The number x is greater than zero.
Awaiting submission
Write a program that reads in many lines of text and counts the number of words and punctuation marks (including: ,.?!). The input ends with the line ***
Awaiting submission

Write a method to the sum of an array of integers.

Template

class TaskA {

    public static void main(String[] args) {
        int[] numbers = Input.intArray();
        int result = sum(numbers);
        System.out.println(result);
    }

    public static int sum(int[] numbers) {
        // TODO: Your answer here
    }

}

You will require Input.java for easy array input.

Awaiting submission

Given an array of strings, return the first word that is longer than 6 letters. If no such word is found then print nothing.

Template

class TaskB {

    public static void main(String[] args) {
        String[] words = Input.stringArray();
        String result = longWord(words);
        System.out.println(result);
    }

    // TODO: Your answer here
}
Awaiting submission
Given an array of integers, return true if all integers in the array are equal.
Awaiting submission
Given an array of strings, return all the words that are longer than 6 letters. Print one per line.
Awaiting submission
A car consists of a model (String) and a speed (int). If the speed is greater than 200 km/h then it is a "fast" car. Complete the Car class by writing 3 methods:
  • getModel() returns the model (String)
  • getSpeed() returns the speed (int)
  • isFast() returns true if the speed is greater than 200 (boolean)

Submit the Car.java file ONLY!

Template:
class Car {
    // Variables
    private String model;
    private int speed;

    // Constructor
    public Car(String model, int speed) {
        this.model = model;
        this.speed = speed;
    }

    // Methods

}
Test code
import java.util.Scanner;

class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Car c = new Car(sc.nextLine(), sc.nextInt());
        System.out.println(c.getModel() + " has a speed of " + c.getSpeed() + "km/h");
        if (c.isFast()) {
            System.out.println("It's a supercar!");
        }
    }
}
Awaiting submission
A seat has a colour and a maximum number of people who can sit down. If the seat can sit more than 1 person then it is a sofa or a bench. Complete the Seat class by writing 3 methods:
  • getColour returns the colour of the seat
  • getMaxPeople returns the maximum number of people who can sit
  • isSofaOrBench returns true if the maximum number of people is greater than 2

Submit the Seat.java file ONLY!

Template:
class Seat {
    private String colour;
    private int maxPeople;

    public Seat(String colour, int maxPeople) {
        this.colour = colour;
        this.maxPeople = maxPeople;
    }
}
Test code:
class TestE {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Seat seat = new Seat(sc.nextLine(), sc.nextInt());
        System.out.print("The " + seat.getColour() + " seat can sit " + seat.getMaxPeople() + " people - ");
        if (seat.isSofaOrBench()) {
            System.out.println("it's a sofa!");
        }
        else {
            System.out.println("not a sofa!");
         }
    }
}
Awaiting submission
A house has a living room, a kitchen, X bedrooms and Y toilets. Complete the House class by writing 3 methods:
  • getBedrooms returns the number of bedrooms X
  • getToilets returns the number of toilets Y
  • getTotal returns the total number of rooms (X + Y + 2)

Submit the House.java file ONLY!

Template:
class House {
    private int bedrooms;
    private int toilets;

    public House(int bedrooms, int toilets) {
        this.bedrooms = bedrooms;
        this.toilets = toilets;
    }
}
Test code:
class TestE {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        House h = new House(sc.nextInt(), sc.nextInt());
        System.out.println("My house has " + h.getTotal() + " rooms including " + h.getBedrooms() + " bedrooms and " + h.getToilets() + " toilets.");
    }
}
Awaiting submission
A t-shirt has a size and a colour. Complete the TShirt class by writing 3 methods:
  • getSize returns the size as a char (S, M, L, X)
  • getColour returns the colour of the t-shirt

Submit the TShirt.java file ONLY!

Template:
class TShirt {
    private char size;
    private String colour;

    public TShirt(char size, String colour) {
        this.size = size;
        this.colour = colour;
    }
}
Test code:
class TestE {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        char size = sc.nextLine().charAt(0);
        String colour = sc.nextLine();
        TShirt fav = new TShirt(size, colour);
        System.out.println("My favorite t-shirt is size " + fav.getSize() + " and " + fav.getColour());
    }
}