Java Slides
Java Slides
Problem:
Write a program that asks for the user's for (String temp: males)
name and then writes that name to the {
monitor with either "Ms." or "Mr." in front,
depending if the name is for a female or if (name.startsWith(temp))
male. Assume that the only female names {
are
System.out.println("Mr. " + name
Amy Buffy Cathy and that the only male + "\n");
names are
found = true;
Elroy Fred Graham All other names will be
break; // stop looping if found
echoed without a title. The program
continues looping until the user hits "enter" }
without first typing a name. }
do
if (name.length() > 0 && !found)
{
{
System.out.print("Enter a name: ");
scan = new Scanner(System.in);
System.out.println("Unknown name
name = scan.nextLine(); entered.\n");
boolean found = false;
(Exercise 1 continuation) for (int i = 0;i < word.length(); i++ ) {
} System.out.println(word.charAt(i));
} while (name.length() > 0); }
scan.close(); }
} }
} Output:
Output: Please Enter a Word :
Enter a name: Group
Amy Johnson G
Ms. Amy Johnson r
o
Enter a name: u
Fred Aguilar p
Mr. Fred Aguilar
Solution:
import java.util.Scanner;
public class LinePerChar {
public static void main(String[] args) {
Scanner input = new
Scanner(System.in);
System.out.println("Please Enter a
Word :");
String word;
word = input.nextLine();
Exercise 3 – Tier pressure System.out.println("Warning:
pressure is out of range\n");
Problem:
}
The front tires of a car should both have the
same pressure. Also, the rear tires of a car System.out.println("Input
should both have the same pressure (but not right rear pressure :");
necessarily the same pressure as the front rrearp = scan.nextInt();
tires.) Write a program that reads in the
pressure of the four tires and writes a if (rrearp < 35 || rrearp > 45)
{
message that says if the inflation is OK or
not.
System.out.println("Warning:
pressure is out of range");
Solution: }
import java.util.Scanner; System.out.println("Input left
public class Tirepressure { rear pressure :");
System.out.println("Input }
right front pressure"); if (rfrontp != lfrontp+2 &&
rfrontp = scan.nextInt(); rrearp+2 != lrearp)
System.out.println("Warning:
Exercise 4 – The pressure is building pressure is out of range");
Problem: }
Tires don't have to have exactly the same System.out.println("Input left
pressure. Modify the program for exercise 3 rear pressure");
so that the front tires can be within 3 psi of
lrp = scan.nextInt();
each other, and the rear tires can be within 3
psi of each other. if (lrp < 35 || lrp > 45) {
Solution:
System.out.println("Warning:
public class LeftRightPres { pressure is out of range");
public static void main(String[] args) { }
int rfp, lfp, rrp, lrp; if (rfp == lfp && rrp == lrp)
Scanner scan = new Scanner
(System.in); System.out.println("Inflation is
OK");
System.out.println("Input
right front pressure"); else
rfp = scan.nextInt();
System.out.println("Inflation is NOT
if (rfp < 35 || rfp > 45) {
OK");
}
System.out.println("Warning:
pressure is out of range"); }
} Output:
System.out.println("Input left Input right front pressure
front pressure");
(Exercise 4 continuation)
lfp = scan.nextInt();
36
if (lfp < 35 || lfp > 45) {
Input left front pressure input2=get.nextInt();
38 System.out.println("Enter 3rd
number:");
Input right rear pressure
input3=get.nextInt();
41
valid1=(input1==firstNum+1)||
Input left rear pressure (input1==firstNum+2)||
43 (input1==firstNum+3)||(input1==firstNum-
1)||(input1==firstNum-2)||
(input1==firstNum-3);
Inflation is OK valid2=(input2==firstNum+1)||
(input2==firstNum+2)||
(input2==firstNum+3)||(input2==firstNum-
Exercise 5 – Combination lock 1)||(input2==firstNum-2)||
(input2==firstNum-3);
Problem:
valid3=(input3==firstNum+1)||
The program simulates a combination lock. (input3==firstNum+2)||
The combination consists of three integers (input3==firstNum+3)||(input3==firstNum-
stored in variables. The user must enter the 1)||(input3==firstNum-2)||
three integers in the correct order. However, (input3==firstNum-3);
each entered integer can be within plus or
minus three of the correct number.
if(valid1 == true && valid2 == true
&& valid3 == true){
Solution: System.out.println("Invalid pin ");
import java.util.Scanner; }
public class CombiLock { else{
public static void main(String[] args) { System.out.println("Lock opens
Scanner get=new Scanner(System.in); ");
int firstNum=10, }
secondNum=20,thirdNum=30; }
int input1,input2, input3; }
boolean valid1, valid2, valid3; Output:
System.out.println("Enter 1st Enter first number :
number:");
13
input1=get.nextInt();
Enter second number :
System.out.println("Enter 2nd
number:"); 17
Enter third number : Random Random_number= new
Random();
31
int
Lock opens right_guess=Random_number.nextInt(10);
int turns=0;
Exercise 6 – Guessing Game Scanner scan=new Scanner(System.in);
Problem: System.out.println("Guess a number
Write a program that implements a guessing between 1 to 10, You will have 3 turns!" );
game: System.out.println("best of luck!");
The program picks a random number
from 1 to 10. Now the user gets three
guesses. As soon as the user enters the int guess;
correct number the program writes a
int i=0;
winning message and exits. If the user fails
to enter the correct number in three guesses, boolean win=false;
the program writes a failure message and
exits. while(win==false) {
if(guess==right_guess) {
int someValue = Math.random() ; //
someValue is between 0.0 and 1.0 win=true;
}
Exercise 8 – Animal
Problem: You must add a sing method to the Bird
class, then modify the main method
Using inheritance, one class can acquire the accordingly so that the code prints the
properties of others. Consider the following following lines:
Animal class:
}
}
(Exercise 8 continuation)
Bird.java
public class Bird extends Animal {
void fly() {
System.out.println("I am flying");
}
void sing() {
System.out.println("I am singing");
}
}
BirdInherit.java
public class BirdInherit {
public static void main(String args[]) {
Bird bird = new Bird();
bird.walk();
bird.fly();
bird.sing();
}
}
Output:
I am walking