Graderpractice for programmers

254275 OOP - Mini Test 1 (Sec 3 & 4) 2562/2

Awaiting submission
Read in an integer, add 3 and print out the result.
Awaiting submission
Read in a sentence (line of words), count how many words in the sentence and print out the result.

If the input is "happy new year everyone" then there are 4 words.

Awaiting submission
Read in 5 float numbers, calculate the average and print out the result to 2 d.p.

Hint: use String.format("%.2f", yourNumber) to convert a float to a String at 2 d.p.

Awaiting submission
Read in a line of integers, put it in an array and sum ONLY THE EVEN numbers. Print out the result.

If the input is 4 7 9 2 then the even numbers are 4 2 so the output is the sum 6.

Starter code:
class D {
    public static void main(String[] args) {
        // Read line of ints into an array
        Scanner sc = new Scanner(System.in);
        String[] line = sc.nextLine().split(" ");
        int[] numbers = new int[line.length];
        for (int i = 0; i < line.length; i++) {
            numbers[i] = Integer.parseInt(line[i]);
        }
		
        // TODO: use a loop to sum the even numbers
    }
}
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());
    }
}