Beginner Programming Lesson Three

Posted on 21st October 2016

Section 0: Intro

This lesson, we're going back to basics a little! So far, you've been learning by copying my code and seeing it work. It's now time for you to write your own! I'm going to set a few Java questions, and help you out by giving a skeleton answer that you have to fill in. You'll be fine I promise! I'll give you a reminder how to compile and run your code in section 1, in case you forgot.

Section 1: Warm Up

Here, I simply want you to be able to enter a number, and have the computer tell you whether that number is odd or even. I'm going to specify the output as follows:

Output: "The number is <odd/even>."

The values in square brackets are to be deleted as appropriate (ie, only print out one of them!).

Here is the skeleton code (save it as Question1.java, it reads in a number from the command line and just prints it out again. You need to alter it so it tells me if the output is odd or even!

 1import java.io.*;
 2public class Question1 {
 3    public static void main(String[] args) {
 4        int num = 0;
 5        System.out.println("Enter a number:");
 6
 7        try {
 8              BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 9            num = Integer.parseInt(br.readLine());
10        } catch (Exception e) {
11            System.out.println("Not a number.");
12            return;
13        }
14
15        System.out.println("You entered: " + num);
16    }
17}

Java

Hint: Think about the mathematical way of telling whether a number is even or odd. Maybe the mod function will be of use? (The % sign in Java).

To compile and run your code, type the following commands into the terminal:

1javac Question1.java
2java Question1

Bash/Shell

Section 2: A Little More Difficult

Next stage, we're going to draw some shapes. I would like you to draw a box of N*N dimensions, where the user can input N.

Output: n*n box with the points as $ characters.

The skeleton code includes the user input for the dimensions of the box, and the first for loop that will draw a single line. You need to make it into a box!

 1import java.io.*;                                                             
 2public class Question2 {                                                      
 3    public static void main(String[] args) {                                  
 4        int boxSize = 0;                                                      
 5        System.out.println("Enter a box size:");                              
 6                                                                              
 7        try {     
 8            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 9           boxSize = Integer.parseInt(br.readLine());    
10        } catch (Exception e) {                                               
11            System.out.println("Not a number.");                              
12            return;
13        }   
14        
15        for (int i = 0; i < boxSize; i++) {                                   
16            System.out.print("$");
17        }   
18        System.out.println();                                                 
19    }   
20}

Java

Hint: Pay attention to your System.out.print() and System.out.println() ordering of commands!

Section 3: Fibonacci Numbers

For this question, you're going to read in a number from the command line, and this number will be the nth fibonacci number to calculate. Let the first Fibonacci number be 1 and the second also be 1. For example:

If you enter 6 into the command line, your program should return 8 because the Fibonacci sequence is as follows:

1 1 2 3 5 8 <-- 6th number is an 8.

In case you haven't met Fibonacci numbers before, just know that the nth number is the sum of the two previous numbers (and the sequence starts with two 1's).

All the sample code does this time is take the user input, it is up to you to calculate that number! You will need to add in extra variables and loops!

 1import java.io.*;                                                             
 2public class Question3 {
 3    public static void main(String[] args) {                                  
 4        int fibnum = 0;
 5        System.out.println("Enter fibonacci number n you want to find:");
 6                                                                              
 7        try {                                                                
 8             BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
 9            fibnum = Integer.parseInt(br.readLine()); 
10        } catch (Exception e) {                                               
11            System.out.println("Not a number.");                              
12            return;
13        }   
14        
15    }   
16}

Java

Hint: Think about how the sum to calculate the current number needs the previous two numbers, and store them in a variable each.

If you get stuck, come and grab me! I'm here to help!

Section 4: Printing Numbers In Words

In this task, I'd like to be able to enter a number between 1 and 1000, and have the computer print that number out to me in word form. Below are some examples:

Input: 345

Output: Your number in words is: three hundred and forty five

Input: 9

Output: Your number in words is: nine

Here is the sample code! It currently only works for numbers 1 to 9. Fix that!

 1import java.io.*;
 2public class Question4 {
 3    public static void main(String[] args) {
 4        int num = 0;
 5        System.out.println("Enter a number between 1 and 1000:");
 6
 7        try {
 8           BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
 9            num = Integer.parseInt(br.readLine());
10        } catch (Exception e) {
11            System.out.println("Not a number.");
12            return;
13        }
14        String number_string = Integer.toString(num);
15
16        System.out.print("You number in words is: ");
17
18        if (number_string.length() == 1) {
19            switch (number_string) {
20                case "1":
21                    System.out.println("one");
22                    break;
23                case "2":
24                    System.out.println("two");
25                    break;
26                case "3":
27                    System.out.println("three");
28                    break;
29                case "4":
30                    System.out.println("four");
31                    break;
32                case "5":
33                    System.out.println("five");
34                    break;
35                case "6":
36                    System.out.println("six");
37                    break;
38                case "7":
39                    System.out.println("seven");
40                    break;
41                case "8":
42                    System.out.println("eight");
43                    break;
44                case "9":
45                    System.out.println("nine");
46                    break;
47            }
48        } // you need to extend this
49
50    }
51}

Java

If you have any trouble, come get me!