oop3
oop3
Software Required:
JDK & Notepad/ Textpad
Example:
int i = 3;
double d;
d = i; //no Explicit type casting required
TASK 1: Write a program which does following conversions and prints the
result. Observe the output.
int i = 3;
double d;
d = i; // OK, no explicit type casting required
// d = 3.0
d = (double) i; // Explicit type casting operator used here
double aDouble = 55; // Compiler auto-casts int 55 to double 55.0
double nought = 0; // Compiler auto-casts int 0 to double 0.0
// int 0 and double 0.0 are different.
Expected Output:
Body Mass Index is 61.30159143458721
TASK 7: Program Rock.java contains a skeleton for the game Rock, Paper,
Scissors. Open it and save it to your directory. Add statements to the program
as indicated by the comments so that the program asks the user to enter a play,
generates a random play for the computer, compares them and announces the
winner (and why). For example, one run of your program might look like this:
$ java Rock
Enter your play: R, P, or S
r
Computer play is S
Rock crushes scissors, you win!
Use a switch statement to convert the randomly generated integer for the computer's play to
a string.
// ****************************************************************
// Rock.java
//
// Play Rock, Paper, Scissors with the user
//
// ****************************************************************
import java.util.Scanner;
import java.util.Random;
public class Rock
{
public static void main(String[] args)
{
String personPlay; //User's play -- "R", "P", or "S"
String computerPlay; //Computer's play -- "R", "P", or "S"
int computerInt; //Randomly generated number used to determine
//computer's play
Scanner scan = new Scanner(System.in);
Random generator = new Random();
//Get player's play -- note that this is stored as a string
//Make player's play uppercase for ease of comparison
//Generate computer's play (0,1,2)
//Translate computer's randomly generated play to string
switch (computerInt)
{
}
//Print computer's play
//See who won. Use nested ifs instead of &&.
if (personPlay.equals(computerPlay))
System.out.println("It's a tie!");
else if (personPlay.equals("R"))
if (computerPlay.equals("S"))
System.out.println("Rock crushes scissors. You win!!");
else
//... Fill in rest of code
}
}
QUESTIONS
Please Fill the blank space with respective answers to following questions:
Question 1: What is different between implicit and explicit type casting?
Implicit type casting is automatic and performed by the compiler without explicit indication, while
explicit type casting requires the programmer to specify the conversion explicitly.
Question 2: Explain each output in TASK 3 one by one in your own words.
4. myInt/5.0 gives: 24.6 • Explanation: Division involving a double, 123 divided by 5.0 is
24.6.
Explanation: Throws a NumberFormatException since " Hello World! " is not a valid
integer. The Integer.parseInt() method expects a string containing a valid integer, and
attempting to parse a non-numeric string results in an exception.
THE END