Solutions

 

 

 

 

Exercise 1

import java.io.*;

public class Exercise1
{
    public static void main (String argv[])
    {
        System.out.println("My name is:");
        System.out.println("Bob Hartley");
        System.out.println("and I'm learning Java.");
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Exercise 2

import java.io.*;

public class Exercise2
{
    public static void main (String argv[])
    {
        System.out.println("My name is:\nBob Hartley\nandI'm learning Java.");
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Exercise 3

import java.io.*;

public class Exercise3
{
    public static void main (String argv[])
    {
        String name;
        double price;
        boolean in_stock;
        int group_number;

        name="Gone With the Wind";
        price=2.15;
        in_stock=true;
        group_number=3;

        System.out.println(name);
        System.out.println("Rents for: $" + price);
        System.out.println("In stock: " + in_stock);
        System.out.println("Group: " + group_number);
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Exercise 4

answers will vary

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Exercise 5

import java.io.*;

public class Exercise5
{
    public static void main (String argv[])
    {
       double x, y;
       x=3;
       y=6;
       System.out.println(x + " + " + y + " = " + (x+y));
       System.out.println(x + " - " + y + " = " + (x-y));
       System.out.println(x + " * " + y + " = " + (x*y));
       System.out.println(x + " / " + y + " = " + (x/y));
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Exercise 6

import java.io.*;

public class Exercise6
{
       public static void main (String argv[])
       {
            double x, y;
            x=3;
            y=6;
            System.out.println(x + " + " + y + " = " + (x+y));
            x++;
            System.out.println(x + " - " + y + " = " + (x-y));
            x--;
            System.out.println(x + " * " + y + " = " + (x*y));
            System.out.println(x + " / " + y + " = " + (x/y));
       }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Exercise 7

import java.io.*;

public class Exercise7
{
        public static void main (String argv[])
       {
                  int age;
                  age=31;
                  if ( age >= 21) 
                      System.out.println( "Eligible to vote ");
                  else 
                      System.out.println("Not eligible.");
        }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Exercise 8

import java.io.*;

public class Exercise8
{
    public static void main (String argv[])
    {
                  string day=”Tuesday”;
                  if ( day == “Saturday” || day == “Sunday” ) 
                      System.out.println( "Weekend");
                  else 
                      System.out.println("Weekday");
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Exercise 9

import java.io.*;

public class Exercise9
{
    public static void main (String argv[])
    {
                  int grade=8;
                  if ( grade < 7 ) 
                      System.out.println( "Elementary");
                  else if ( grade < 9 )
                      System.out.println("Junior High");
                  else 
                      System.out.println("High School");
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Exercise 10

import java.io.*;

public class Exercise10
{
    public static void main (String argv[])
    {            
       int  year_of_birth = 1960; 
       switch (  year_of_birth  %  12  )
      
          case 0: 
            System.out.println("You were born in “ + year_of_birth + “, the year of the Monkey"); 
            break; 
          case 1: 
            System.out.println("You were born in “ + year_of_birth + “, the year of the Rooster"); 
            break; 
          case 2: 
            System.out.println("You were born in “ + year_of_birth + “, the year of the Dog"); 
            break; 
          case 3: 
            System.out.println("You were born in “ + year_of_birth + “, the year of the Pig"); 
            break; 
          case 4: 
            System.out.println("You were born in “ + year_of_birth + “, the year of the Rat"); 
            break; 
          case 5: 
            System.out.println("You were born in “ + year_of_birth + “, the year of the Ox"); 
            break; 
          case 6: 
            System.out.println("You were born in “ + year_of_birth + “, the year of the Tiger"); 
            break; 
          case 7: 
            System.out.println("You were born in “ + year_of_birth + “, the year of the Rabbit"); 
            break; 
          case 8: 
            System.out.println("You were born in “ + year_of_birth + “, the year of the Dragon"); 
            break; 
          case 9: 
            System.out.println("You were born in “ + year_of_birth + “, the year of the Snake"); 
            break; 
          case 10: 
            System.out.println("You were born in “ + year_of_birth + “, the year of the Horse"); 
            break; 
          case 11: 
            System.out.println("You were born in “ + year_of_birth + “, the year of the Sheep"); 
            break; 
         default: 
            System.out.println("Error"); 
            break; 
       }
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Exercise 11

import java.io.*;

public class Exercise11
{
    public static void main (String argv[])
    {
            for ( int i = 10;  i > 0;  i-- ) 
            {
                        System.out.println ( i );
            }
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Exercise 12

import java.io.*;

public class Exercise12
{
    public static void main (String argv[])
    {
             int i = 10;
             while  ( i > 0 )
            {
                        System.out.println ( i );
                        i--;
            }
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Exercise 13

import java.io.*;

public class Exercise13
{
    public static void main (String argv[])
    {
             int i = 10;
            do
            {
                        System.out.println ( i );
                        i--;
            }  while  ( i > 0 ) ;
    }
}