Java

TOPIC 14 – IF STATEMENTS – PART 1

 

LESSON WORK

 

 

QUESTION 1

What will the following programs output?

 

a)
int c = 10;

System.out.print(c == 10);

b)
boolean cool = true;

System.out.print(cool);

 

c)
int z = 14;

System.out.print(z >= 15);

 

d)
String s = "hello";

System.out.print(s == "hello");

 

e)
String s = "hello";

System.out.print(s.equals("hello"));

 

f)

int t = 9;

if (t != 10)

{

   System.out.print("A");

}

System.out.print("B");

 

g)

int coco = 22;

if (coco < 100)

{

   System.out.print("A");

}

else

{

   System.out.print("B");

}

 

h)

double x = 22.9;

if (Math.floor(x) > 22.0)

{

   System.out.print("A");

}

else

{

   System.out.print("B");

}

System.out.print("C");

 

i)

String s = "Patrick";

if (s.equals("Pat"))

{

   System.out.print("A");

}

else

{

   System.out.print("B");

}

 

j)

int z = 32;

if (z >= 32)

{

   System.out.print("A");

}

System.out.print("B");

 

k)

if (3 > 5)

{

   System.out.print("A");

}

else

{

   System.out.print("B");

}

QUESTION 2

 

Write a program that asks the user for an integer.  The program outputs "Positive Number" if the number is greater or equal to zero.  Otherwise, the program outputs "Negative Number".

 

QUESTION 3

 

Write a program that asks the user for an integer.  It then outputs whether the integer is odd or even.  Remember that even numbers are 0, 2, 4, 6, 8, … while odd numbers are 1, 3, 5, 7, …

 

QUESTION 4

 

Write a program that asks the user for his/her first name in lowercase letters.  The program then outputs “Great to see you name” if the name is yours.  Otherwise, it outputs “Oh it's you”.

 

Here is sample output for Mr. Campeau’s program:

 

          Sample #1

 

     Enter your name:

     Patrick

     Great to see you Patrick!

 

          Sample #2

 

     Enter your name:

     Bryce

     Oh, it's you!

 

QUESTION 5

 

You will create a program that simply asks the user to provide information about themselves.  However, it will also give you the option for the user to be a preset individual.  To do this, you are expected to create a Player class to store this information inside.


STEP 1

Create a Player class that simply contains the three instance variables:

  • String firstName;
  • String lastName;
  • int age;


STEP 2

Create a class called Hello.  Inside the main method, the code asks the user if their name is Pat (or any other name you want to choose).  The user is to enter y or n .  If the answer is y, the program simply creates an instance of Player with information for Pat.  Otherwise, the program goes on to ask the user for their first name, last name and age and creating a Player instance.

 

Finally, the program simply outputs the Player’s information to screen.

 

 

SAMPLE I/O #1

 

Hello.  Is this the great Pat? (y/n)

y

 

Player information:

Patrick Campeau
Age: 32

 

SAMPLE I/O #2

 

Hello.  Is this the great Pat? (y/n)

n

What is your first name?

Ava
What is your last name?

Bard
What is your age?

21

 

Player information:

Ava Bard
Age: 21