0% found this document useful (0 votes)
16 views

Chapter 03

The document discusses Java programming concepts like if/else statements, logical operators, and switch statements. It provides code examples to demonstrate these concepts, like an addition quiz that checks the user's answer, use of if/else to check digits in a lottery number, and examples using switch statements to assign a letter grade based on a test score. It concludes with homework problems applying these concepts, like calculating grade averages, checking for rectangles, finding greatest of two numbers, and printing weekdays from numbers.

Uploaded by

Nader Azal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Chapter 03

The document discusses Java programming concepts like if/else statements, logical operators, and switch statements. It provides code examples to demonstrate these concepts, like an addition quiz that checks the user's answer, use of if/else to check digits in a lottery number, and examples using switch statements to assign a letter grade based on a test score. It concludes with homework problems applying these concepts, like calculating grade averages, checking for rectangles, finding greatest of two numbers, and printing weekdays from numbers.

Uploaded by

Nader Azal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

CHAPTER 3

LISTING 3.1 AdditionQuiz.java


import java.util.Scanner;

public class AdditionQuiz {


public static void main(String[] args) {
int number1 = (int)(System.currentTimeMillis() % 10);
int number2 = (int)(System.currentTimeMillis() / 7 % 10);

// Create a Scanner
Scanner input = new Scanner(System.in);

System.out.print("What is " + number1 + " + " + number2 + "?


");

int answer = input.nextInt();

System.out.println( number1 + " + " + number2 + " = " +


answer + " is " + (number1 + number2 == answer));
}
}
3.3 if Statements
if (boolean-expression)
{
statement(s);
}

LISTING 3.2 SimpleIfDemo.java


import java.util.Scanner;

public class SimpleIfDemo {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter an integer: ");
int number = input.nextInt();

if (number % 5 == 0)
System.out.println("HiFive");

if (number % 2 == 0)
System.out.println("HiEven");

if (number % 2 == 1)
System.out.println("HiOdd");
}
}

3.4 Two-Way if-else Statements


if (boolean-expression) {
statement(s)-for-the-true-case;
}
else {
statement(s)-for-the-false-case;
}

if (score >= 90.0)


System.out.print("A");
else
if (score >= 80.0)
System.out.print("B");
else
if (score >= 70.0)
System.out.print("C");
else
if (score >= 60.0)
System.out.print("D");
else
System.out.print("F");

LISTING 3.3 SubtractionQuiz.java


import java.util.Scanner;

public class SubtractionQuiz {


public static void main(String[] args) {
// 1. Generate two random single-digit integers
int number1 = (int)(Math.random() * 10);
int number2 = (int)(Math.random() * 10);

// 2. If number1 < number2, swap number1 with


number2
if (number1 < number2) {
int temp = number1;
number1 = number2;
number2 = temp;
}
// 3. Prompt the student to answer ”What is number1 –
number2?”
System.out.print ("What is " + number1 + " - " + number2 +
"? ");
Scanner input = new Scanner(System.in);
int answer = input.nextInt();
// 4. Grade the answer and display the result
if (number1 - number2 == answer)
System.out.println("You are correct!");
else {
System.out.println("Your answer is wrong.");
System.out.println(number1 + " - " + number2 + " should
be " + (number1 - number2));
}
}
}

3.10 Logical Operators


TABLE 3.3 Boolean Operators
Operator Name Description
 !
   &&
    ||
  ^
LISTING 3.8 Lottery.java
import java.util.Scanner;

public class Lottery {


public static void main(String[] args) {
// Generate a lottery number
int lottery = (int)(Math.random() * 100);

// Prompt the user to enter a guess


Scanner input = new Scanner(System.in);
System.out.print("Enter your lottery pick (two digits): ");
int guess = input.nextInt();

// Get digits from lottery


int lotteryDigit1 = lottery / 10;
int lotteryDigit2 = lottery % 10;

// Get digits from guess


int guessDigit1 = guess / 10;
int guessDigit2 = guess % 10;

System.out.println("The lottery number is " + lottery);


// Check the guess
if (guess == lottery)
System.out.println("Exact match: you win $10,000");
else if (guessDigit2 == lotteryDigit1 && guessDigit1 ==
lotteryDigit2)
System.out.println("Match all digits: you win $3,000");
else if (guessDigit1 == lotteryDigit1 || guessDigit1 ==
lotteryDigit2
|| guessDigit2 == lotteryDigit1
|| guessDigit2 == lotteryDigit2)
System.out.println("Match one digit: you win $1,000");
else
System.out.println("Sorry, no match");
}
}
3.13 switch Statements
A switch statement executes statements based on the
value of a variable or an expression

switch (switch-expression)
{
case value1: statement(s)1; break;
case value2: statement(s)2; break;
...
case valueN: statement(s)N; break;
}
default: statement(s)-for-default;

Example 1 of Switch
import java.util.Scanner;

public class Switch05{


public static void main(String[] args)
{
int score;
String grade = "";

Scanner in = new Scanner(System.in);

System.out.print("Enter your score here: ");


score = in.nextInt();

switch ( score )
{
case 100:
case 99:
case 98:
case 97:
case 96:
case 95:
case 94:
case 93:
case 92:
case 91:
case 90: grade = "A"; break;
case 89:
case 88:
case 87:
case 86:
case 85:
case 84:
case 83:
case 82:
case 81:
case 80: grade = "B"; break;

case 79:
case 78:
case 77:
case 76:
case 75:
case 74:
case 73:
case 72:
case 71:
case 70: grade = "C"; break;

case 69:
case 68:
case 67:
case 66:
case 65:
case 64:
case 63:
case 62:
case 61:
case 60: grade = "D"; break;

default: grade = "F"; break;


}

System.out.println("Your grade is " + grade);


}
}
Example 2 of Switch
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter your score here: ");
int score = in.nextInt();
String grade;
switch (score / 10) {
case 10:
case 9: grade = "A"; break;
case 8: grade = "B"; break;
case 7: grade = "C"; break;
case 6: grade = "D"; break;
default: grade = "F"; break;
}
System.out.println("Your grade is " + grade);
}

H.W
1- Accept 5 marks of a student find the total and average marks. And find the grade
according to the following conditions

if average mark >= 90 Grade is Ex

if average mark >= 80 Grade is A

if average mark >= 70 Grade is B

if average mark >= 60 Grade is C

if average mark >= 50 Grade is D

if average mark >= 40 Grade is E

else Grade is E

2- Take values of length and breadth of a rectangle from user and check if it is
square or not.
3-Take two int values from user and print greatest among them.

4-A shop will give discount of 10% if the cost of purchased quantity is more
than 1000.
Ask user for quantity
Suppose, one unit will cost 100.
Judge and print total cost for user.

5- A company decided to give bonus of 5% to employee if his/her year of


service is more than 5 years.
Ask user for their salary and year of service and print the net bonus amount.

6-Take input of age of 3 people by user and determine oldest and youngest
among them.

7-Write a program to print absolute value of a number entered by user. E.g.-


INPUT: 1 OUTPUT: 1
INPUT: -1 OUTPUT: 1

8- print weekday’s name according to given weekday’s number. In this


program we will read weekday’s number between 1 to 7 and print weekday’s
name. using Switch Statement.

You might also like