University of Ottawa

School of IT and Engineering

CSI1102

 

Lab Assignment 1: Solution

 

 

 

1.      Design and implement an application called Hello.java which prints the phrase “Hello CSI 1102, Winter 2005!!!” on one line and the phrase “Enjoy the course and have fun!” on another line.

 

//  Hello.java      

//  Demonstrates the basic structure of a Java application.

//********************************************************************

 

public class Hello

{

   //-----------------------------------------------------------------

   //  Prints two lines.

   //-----------------------------------------------------------------

   public static void main (String[] args)

   {

      System.out.println ("Hello CSI1102, Winter 2005!!!");

      System.out.println ("Enjoy the class and have fun!");

 

   }

}

 

2.      Design and implement an application Password.java that produces and prints a random password. The password is composed of random digits 0-9 and characters a-z. The first element is a character, the second is a digit, then a character, a digit, a character, a digit, a character, and a digit (8 elements in total). An example is: e5c8a0b6.

 

//********************************************************************

//  Password.java      

//********************************************************************

 

import cs1.Keyboard;

 

public class Password

{

   //-----------------------------------------------------------------

   //  Produces a random password of 8 characters and digits.

   //-----------------------------------------------------------------

   public static void main (String[] args)

   {

      String result = "";

 

      for (int i=0; i<=3; i++)

       {

        result += (char) (Math.random() * 26 + 'a');

        result += (int) (Math.random() * 10);

       }

 

      System.out.println ("The password is: " + result);

   }

}

 

 

3.      Design and implement an application Current.java that computes the intensity of the electrical current in a resistor of resistance R, when the applied voltage is V. Ask the user to input the value of R (in ohms) and the values of V (in volts). Then print the value of the intensity I, computed with the formula I = V / R. Ask the user if she/he wants to repeat the process, and allow it while the answer is “y” or “Y”. This question will familiarize you with reading input from the keyboard, using the class cs1.Keyboard provided by the authors of the textbook. (Don’t forget to run your program with “Run with console” instead of “Run application”.)

 

//********************************************************************

// Current.java

//********************************************************************

 

import cs1.Keyboard;

 

public class Current

{

  //-----------------------------------------------------------------

  //  Computes the intensity of an electrical current

  //-----------------------------------------------------------------

  public static void main (String[] args)

  {

   double R, V;

   String again;

   do

   {

    System.out.print("Input the value of the resistance (in ohms) ");

    R = Keyboard.readDouble();

    System.out.print("Input the value electric potential (in volts) ");

    V = Keyboard.readDouble();

    System.out.println("The intensity of the electrical current is: " +

                        V/R + " amperes.");

       

    System.out.println();

    System.out.print ("Do you want to do this again (y/n)?: ");

    again = Keyboard.readString();

   }

   while (again.equalsIgnoreCase ("y"));

  }

}

 

 

4.      Design and implement an application called MonsterGame.java that simulates a simple game.  You have three magic arrows to kill an evil monster. When you fire an arrow, the program generates a random integer between 0 and 10 (including 0 and 10).  This number shows how much damage you did to the monster. The monster has 10 energy points initially. After each arrow is fired (by simply printing a message that says it was fired and shows the random number), the monster’s energy is decreased by the generated number (but not decreased below zero). After you fire the first arrow, if the monster’s energy is zero, print the message “You won!!!”; otherwise you fire the second arrow. After you fire the second arrow, if the monster’s energy is zero, you win; otherwise you fire the third arrow. If in the end the energy is zero, you win; otherwise, print the message: “Nice try. Almost there!”. After each arrow is fired, print a message with the current energy level left for the monster. After the game is over, print the message “Do you want to play again? (y/n)”, and allow the user to play as many times as desired.

 

//********************************************************************

//  MonsterGame.java

//  Simulates a simple game

//********************************************************************

 

import cs1.Keyboard;

 

public class MonsterGame

{

   //-----------------------------------------------------------------

   //  Plays a game of defeating an evil monster using 3 magic arrows

   //-----------------------------------------------------------------

   public static void main (String[] args)

   {

      final int ARROW_N = 11;

      int arrow, energy;

      final int ENERGY_CONST = 10;

      String again;

 

      do

      {  energy = ENERGY_CONST;

         System.out.println("The monster has "+energy+" points left.");

         System.out.print("Fire your first arrow: ");

         arrow = (int) (Math.random() * ARROW_N);

         System.out.println(arrow);

         energy = Math.max(energy - arrow,0);

         System.out.println("The monster has " + energy +

                            " points left.");

        

         if(energy > 0)

          {

            System.out.print("Fire your second arrow: ");

           arrow = (int) (Math.random() * DIGITS);

           System.out.println(arrow);

           energy = Math.max(energy - arrow,0);

           System.out.println("The monster has " + energy +

                              " points left.");

          }

         

         if (energy > 0)

          {

            System.out.print("Fire your third arrow: ");

           arrow = (int) (Math.random() * DIGITS);

           System.out.println(arrow);

           energy = Math.max(energy - arrow,0);

           System.out.println("The monster has " + energy +

                              " points left.");

          }

         if (energy > 0)

           System.out.println ("Nice try; almost there!");

         else

           System.out.println ("You won!");

 

         System.out.println();

         System.out.print ("Play again (y/n)?: ");

         again = Keyboard.readString();

      }

      while (again.equalsIgnoreCase ("y"));

   }

}