Beruflich Dokumente
Kultur Dokumente
nine lines tall. Each big letter should be made up of a bunch of *'s. For
example, if your initials were "DJE", then the output would look something
like:
****** ************* **********
** ** ** **
** ** ** **
** ** ** **
** ** ** ********
** ** ** ** **
** ** ** ** **
** ** ** ** **
***** **** **********
Solution
} // end class
2) Write a program that asks the user's name, and then greets the user by
name. Before outputting the user's name, convert it to upper case letters. For
example, if the user's name is Lucy, then the program should respond "Hello,
LUCY, nice to meet you!"
Solution
public class Greeting {
upperCaseName = usersName.toUpperCase();
} // end main()
} // end class
A version using Scanner for input:
import java.util.Scanner;
upperCaseName = usersName.toUpperCase();
} // end main()
} // end class
3) Write a program that helps the user count his change. The program should
ask how many 100 birr notes the user has then how many 50 birr notes, then
how many 10 birr notes, then how many 5 birr notes, then how many 1 birr
notes, then how many 50 cents, then how many 25 cents, then how many 10
cents, then how many 5 cents. Then the program should tell the user how
much money he has, expressed in birr.
Solution
System.out.println();
System.out.print("The total in dollars is $");
System.out.printf("%1.2f", dollars); // Formatted output!
System.out.println();
} // end main()
} // end class
A version using Scanner for input:
import java.util.Scanner;
System.out.println();
System.out.print("The total in dollars is $");
System.out.printf("%1.2f", dollars); // Formatted output!
System.out.println();
} // end main()
} // end class
4) If you have N eggs, then you have N/12 dozen eggs, with N%12 eggs left
over. (This is essentially the definition of the / and % operators for integers.)
Write a program that asks the user how many eggs she has and then tells the
user how many dozen eggs she has and how many extra eggs are left over. a.
A gross of eggs is equal to 144 eggs. Extend your program so that it will tell
the user how many gross, how many dozen, and how many left over eggs she
has. For example, if the user says that she has 1342 eggs, then your program
would respond with Your number of eggs is 9 gross, 3 dozen, and 10 since
1342 is equal to 9*144 + 3*12 + 10.
Solution
} // end main()
} // end class
Improved version:
public class GrossAndDozens {
} // end main()
} // end class
5) Write a program that allows the user to enter the grade scored in a java class
(0-100). If the user scored a 100 then notify the user that they got a perfect
score. It should also print the student grade based on the following scales 0-
59 F 60-69 D 70-79 C 80-89 B 90-100 A a. It should print an error nun
numeric value not allowed if the user enters a character b. It should print an
error A student grade cannot be Negative if the user enters a negative
value. c. It should print an error A student grade cannot exceed maximum if
the grade is greater than 100
Solution
int main()
{
int number;
switch (number)
{
case '1':
cout << "You chose Coke";
break;
case '2':
cout << "You chose Dr. Pepper";
break;
case '3':
cout << "You chose Water";
break;
case '4':
cout << "You chose Sprite";
break;
case '5':
cout << "You chose Lemonade";
break;
default:
cout << "Error: Choice was not valid. Here is your money back.";
#include <iostream>
#include <stdlib.h>
int main()
{
int compnumber;
string ok;
cout << "Think of a number between 1 and 100. Type 'ok' and I'll try and guess it." << endl;
srand(time(NULL));
}
cout << "I won!" << endl;
}
Other code
#include <iostream>
#include <ctime>
#include <string>
using namespace std;
int main() {
cout << "Enter a number between 1-100 for computer to try and guess
it\nNumber: ";
cin >> userNumber;
if(computerNumber<userNumber){
mode = 0;//if computer number is smaller than user number do
case 0
}
else if (computerNumber>userNumber){
mode = 1;//if computer number is bigger than user number do
case 1
}
divider = computerNumber;
switch(mode){
case 0:
while(computerNumber != userNumber){
times++;
cout << computerNumber;
cout << ":";
cin >> directions;
case 1:
while(computerNumber != userNumber){
times++;
cout << computerNumber;
cout << ":";
cin >> directions;
divider /= 2;
computerNumber += divider;
if(computerNumber == 0){//prevents
computer from "saying" 0 because the task was to find a number between 1-100
computerNumber++;
}
}
}
break;
cout << computerNumber << ": Win!" << endl << endl;
cout << "Computer found the number, the number was " << computerNumber
<< "!" << endl;
cout << "It took " << times << " attempts for computer to guess it!"
<< endl;
}
Other code1
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int random, guess, counter = 0;
srand((unsigned) time(0));
random = (rand()%3)+1;
do{
cout << " Guess the random number: ";
cin >> guess;
counter++;
return 0;
8) In the above exercise modify the program so that instead of the user
guessing a number the computer came up with, the computer guesses the
number that the user has secretly decided. The user must tell the computer
whether it guessed too high or too low. a. Modify the program so that no
matter what number the user thinks of (1-100) the computer can guess it in 7
or less guesses.
Solution
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
int main()
{
srand(time(NULL));
do
{
cout << "Guess a random number from 1-100: ";
cin >> guess;
/**************************************************/
cout << endl << endl << "Computer Section" << endl;
cout << "Enter a number you want the computer to guess: ";
cin >> guess;
found = false;
guessCount = 1;
do
{
randomNum = rand() % 100 + 1;
/**************************************************/
cout << endl << endl << "Computer Section 2.0" << endl;
cout << "Here the computer should guess much faster(less than 7)" << endl;
cout << "Enter a number you want the computer to guess: ";
cin >> guess;
found = false;
int maxRange = 100;
int lowRange = 1;
int newHigh = 0;
guessCount = 1;
do
{
randomNum = rand() % maxRange + lowRange;
if (guess == randomNum)
{
found = true;
cout << "Congratulation you found the number in " <<
guessCount << " guess" << endl;
}
else if (randomNum < guess)
{
cout << "Too Low: " << randomNum<< endl;
newHigh = maxRange + lowRange;
lowRange = randomNum+1;
guessCount++;
cout << endl;
} while (!found);
return 0;
}
9) Make a two player tic tac toe game
a.The program should announce when a player has won the
game (and which player won, x or o) b. The program should
block any further moves after one of the party has won the
game.
Solution
public class TicTacToe {
public TicTacToe() {
board = new char[3][3];
currentPlayerMark = 'x';
initializeBoard();
}
return isFull;
}
// Check to see if all three values are the same (and not
empty) indicating a win.
private boolean checkRowCol(char c1, char c2, char c3) {
return ((c1 != '-') && (c1 == c2) && (c2 == c3));
}
//////////////
// Create game and initialize it.
// First player will be 'x'
TicTacToe game = new TicTacToe();
// Player 'x' places a mark in the top right corner row 0, column
2
// These values are based on a zero index array, so you may need
to simply take in a row 1 and subtract 1 from it if you want
that.
game.placeMark(0,2);
}
public static void move1(String[][] Board, Scanner scan){
int Input = scan.nextInt();
for (int i = 0;i<100;i++){
if (Input == 1){
Board[0][1]="X";
grid1(Board, scan);
move2(Board,scan);
} else if(Input == 2){
Board[3][1]="X";
grid1(Board,scan);
move2(Board,scan);
} else if(Input == 3){
Board[0][2]="X";
grid1(Board,scan);
move2(Board,scan);
} else if(Input == 4){
Board[1][1]="X";
grid1(Board,scan);
move2(Board,scan);
} else if(Input ==5){
Board[3][2]="X";
grid1(Board,scan);
move2(Board,scan);
} else if(Input ==6){
Board[1][2]="X";
grid1(Board,scan);
move2(Board,scan);
} else if(Input ==7){
Board[3][3]="X";
grid1(Board,scan);
move2(Board,scan);
} else if(Input == 8){
Board[2][1]="X";
grid1(Board,scan);
move2(Board,scan);
} else if(Input ==9){
Board[2][2]="X";
grid1(Board,scan);
move2(Board,scan);
}
}
}
public static void move2(String[][] Board, Scanner scan) {
System.out.println("Where do you wan't your O to be?");
int Input = scan.nextInt();
if (Input == 1){
Board[0][1]="O";
grid1(Board, scan);
move3(Board, scan,Input);
} else if(Input == 2){
Board[3][1]="O";
grid1(Board,scan);
move3(Board, scan,Input);
} else if(Input == 3){
Board[0][2]="O";
grid1(Board,scan);
move3(Board, scan,Input);
} else if(Input == 4){
Board[1][1]="O";
grid1(Board,scan);
} else if(Input ==5){
Board[3][2]="O";
grid1(Board,scan);
move3(Board, scan,Input);
} else if(Input ==6){
Board[1][2]="O";
grid1(Board,scan);
move3(Board, scan,Input);
} else if(Input ==7){
Board[3][3]="O";
grid1(Board,scan);
move3(Board, scan,Input);
} else if(Input == 8){
Board[2][1]="O";
grid1(Board,scan);
move3(Board, scan,Input);
} else if(Input ==9){
Board[2][2]="O";
grid1(Board,scan);
move3(Board, scan,Input);
}
}
public static void move3(String[][] Board, Scanner scan, int Input){
if (Input == 1){
Board[0][1]="X";
grid1(Board, scan);
} else if(Input == 2){
Board[3][1]="X";
grid1(Board,scan);
} else if(Input == 3){
Board[0][2]="X";
grid1(Board,scan);
} else if(Input == 4){
Board[1][1]="X";
grid1(Board,scan);
} else if(Input ==5){
Board[3][2]="X";
grid1(Board,scan);
} else if(Input ==6){
Board[1][2]="X";
grid1(Board,scan);
} else if(Input ==7){
Board[3][3]="X";
grid1(Board,scan);
} else if(Input == 8){
Board[2][1]="X";
grid1(Board,scan);
} else if(Input ==9){
Board[2][2]="X";
grid1(Board,scan);
}
}
}
10) Change the above program so that the program becomes a one
player game against the computer (with the computer making
its moves randomly)