web viewcs110 programming. language i. lab 5 : java basics ii. if else. computer science...
TRANSCRIPT
CS110 Programming
Language I
Lab 5 : Java basics II
if else
Computer Science Department
Fall 2016
Lab#4 : ifelse
Lab Objectives:
In this lab, the student will practice:
Defining Boolean variables
Writing and evaluating boolean expressions
Using if..else as a branching mechanism
Lab Exercise 1: Programming Output
What will happen when you attempt to compile and run the following code? Please correct any error if there is any? (Whether it is logical error or compilation one)
1.
public class Test
{
public static void main( String[] args )
{
boolean b = false;
int x = 19;
int y = 1;
if( b = true || x > y)
System.out.println("b has a true value");
(P a g e |2)
else
}
)
System.out.println("b has a false value " );
Code output:
Fixed error:
2.
public class Test
{
public static void main( String[] args )
{
int Distance = 90;
if (Distance >=80)
System.out.println("The distance is more than 80 KM");
else
}
)
System.out.println( "sorry!!" );
System.out.println("The distance is less than 80 KM");
Code solution:
Fixed error:
Lab Exercise 2: Draw a Flowchart and Write
Problem Description
Design a flowchart and write a java program to find the smallest of three numbers A, B, and C. [Note: use if statement]
Sample output
Please enter three numbers >> 5 6 10
5 is the smallest among the numbers 5 6 and 10
Algorithm
Code:
Follow-up Questions and Activities
Modify the program to use the ternary conditional operator
Lab Exercise 3:Positive or Negative
Problem Description
Write a java program that inputs a character value and decides if it is capital letter, small letter or digit. (Hint: convert the character to ascii code number. use (http://macao.communications.museum/images/exhibits/2_18_8_1_eng.png)
Sample output
Enter the character: R
The character you entered is a Capital letter
Code
Assignment Problem(s)
Q1: Write a Java program (use If statement ) that recommends the number of calories a person should eat each day. Base your recommendation on the person's weight and whether the person has an active or sedentary (inactive) lifestyle.:
If the person is sedentary, that person's activity factor is 13.
If the person is active, that person's activity factor is 15.
Recommended number of calories= weight* activity factor
*User enter whether they have active or sedentary lifestyle, as a character, 'A' for active or 'S' sedentary
Hint:
To read char from the user use this method: input.next().cahrAt(0);
Output sample:
Enter you weight in pound:103
Do you have active or sedentary lifestyle :S
The recommended calories for you:1339 calories
Code
Q2: Write a Java program (use if statement) that get a traffic violation number and output the traffic violation title and price based on the following table:
Number
Title
Price
1
Not stopping at (STOP) signs
SR 100
2
Usage of improper light
SR 100
3
Sudden start-up (heeling)
SR 200
4
Violating speed limit
SR 100
5
Leaving the car unattended on public streets
SR 100
6
Destroying the Denver boot
SR 1500
else
undefined
undefined
Output sample:
Enter traffic violation number:4
Traffic violation title : Violating speed limit
Price= SR 100
Code