Java Slides

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Exercise 1 – Salutation Applier

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. }

Solution: if (found) { continue; }


import java.util.Scanner; for (String temp: females)
public class SalutationApplier { {
public static void main(String[] args) if (name.startsWith(temp))
{ {
String name; System.out.println("Ms. " + name
+ "\n");
String[] males = {"Elroy", "Fred ", "
Graham"}; found = true;
String[] females = {"Amy", "Buffy", break;
"Cathy"};
}
Scanner scan = null;
}

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

Exercise 2 – Line per Character


Problem:
Write a program where the user enters a
string, and the program display it to the
monitor with one character per line:

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 :");

public static void main(String[] args) { lrearp = scan.nextInt();

int rfrontp, lfrontp, rrearp , if (lrearp< 35 || lrearp > 45) {


lrearp;
Scanner scan = new Scanner System.out.println("Warning:
(System.in); pressure is out of range\n");

System.out.println("Input }
right front pressure"); if (rfrontp != lfrontp+2 &&
rfrontp = scan.nextInt(); rrearp+2 != lrearp)

if (rfrontp < 35 || rfrontp > System.out.println("Inflation is


45) { NOT OK");
else
System.out.println("Warning: System.out.println("Inflation is
pressure is out of range\n"); OK");
} }
System.out.println("Input left }
front pressure :");
Output:
lfrontp = scan.nextInt();
Input right front pressure
if (lfrontp < 35 || lfrontp > 45)
{ 37
Input left front pressure
(Exercise 3 continuation)
37
System.out.println("Warning:
Input right rear pressure pressure is out of range");
43 }
Input left rear pressure System.out.println("Input
43 right rear pressure");
rrp = scan.nextInt();

Inflation is OK if (rrp < 35 || rrp > 45) {

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) {

You will need Math.random(), which guess=scan.nextInt();


produces a random double between 0.0 and turns++;
1.0. For example:

if(guess==right_guess) {
int someValue = Math.random() ; //
someValue is between 0.0 and 1.0 win=true;
}

You will need to use some other methods of else if(i>2){


the Math class along with some arithmetic to System.out.println("You loose! the right
convert this into a random integer in the answer was: "+right_guess);
desired range. Look in your Java
documentation for further details. Here are return;
two example runs of the program:
}
else if(guess<right_guess){
Solution:
i++;
import java.util.Random;
System.out.println("Yor Guess is lower
import java.util.Scanner; than the right guess! Turns left: "+(3-i));
public class GuessingGame { }
public static void main(String[] args) { else if(guess>right_guess) {
i++; Write a program that writes out the greeting
as many times as there are characters in the
System.out.println("Your Guess Is greeting. The Greeting class will have a
Higher Than THe Right Guess! Turns left: constructor that allows the main() method to
"+(3-i)); initialize objects to different greetings.
}
(Exercise 6 continuation) Solution:
System.out.println("You win!");
class Greetings {
System.out.println("Then number was
private String greeting = "hello";
"+right_guess);
System.out.println("You used "+turns+"
turns to guess the right number"); public Greetings(String nameLength)
{
System.out.println("Your score is "+((11-
turns)*10)+" out of 100"); this.greeting=greeting;
} }
}
} public void setGreet(String greeting)
{
this.greeting = greeting;
Output:
}
I am thinking of a number from 1 to 10.
You must guess what it is in three tries.
Enter a guess:
public String getGreet(){
1
for(int i = 0; i <
wrong greeting.length(); i++){
5
System.out.println(greeting);
wrong
}
9
return greeting;
wrong
}
The correct number was 7. You have lost
the game. }

Exercise 7 – Greeting public class Greeting{


Problem: public static void main(String[] args)
{
Greetings greet = new
Greetings("Hello");
public class Solution{ public static void
greet.getGreet(); main(String[] args){
}
} Bird bird = new Bird(); bird.walk();
bird.fly(); } }
(Exercise 7 continuation)
Output:
The above code will print:
hello
hello
I am walking I am flying
hello
hello
This means that a Bird object has all the
hello properties that an Animal object has, as well
as some additional unique properties.

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:

I am walking I am flying I am singing


class Animal{ void walk()
{ System.out.println("I am walking"); Animal.java
}} class Animal{
void walk() {
This class has only one method, walk. Next, System.out.println("I am walking");
we want to create a Bird class that also has a
fly method. We do this using extends }
keyword: }

class Bird extends Animal { void fly() { Solution:


System.out.println("I am flying"); } }
Animal2.java
public class Animal2{
Finally, we can create a Bird object that can
both fly and walk. void walk()
{ I am flying
System.out.println("I am walking"); I am singing

}
}

(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

You might also like