You are on page 1of 10

/*

* Hey, if you like this, can you like my fb page? I have no idea how to promote
it.
* https://www.facebook.com/MetonymyQT
* I won't spam you, thanks! (you can turn notifications off.)
*/
import java.util.Arrays;
import java.util.Scanner;
public class SetProbleme2 {
public static void main(String[] args) {
// problema1();
// problema2();
// problema3();
// problema4();
// problema5();
// problema6();
// problema7();
// problema8();
// problema9();
problema10();
}
static void problema1() {
/*
* (Algebra: solve quadratic equations) The two roots of a quadr
atic
* equation ax 2 +bx+c =0 can be obtained using the following fo
rmula: r
* 1 = -b+2b 2 -4ac 2a and r 2 = -b-2b 2 -4ac 2a b 2 -4acis call
ed the
* discriminant of the quadratic equation. If it is positive, th
e
* equation has two real roots. If it is zero, the equation has
one
* root. If it is negative, the equation has no real roots. Writ
e a
* program that prompts the user to enter values for a, b, and c
and
* displays the result based on the discriminant. If the discrim
inant is
* positive, display two roots. If the discriminant is 0, displa
y one
* root. Otherwise, display The equation has no real roots. Note t
hat
* you can use Math.pow(x, 0.5)to compute 2x. Here are some samp
le runs.
*/
Scanner input = new Scanner(System.in);
System.out.println("Enter a, b, c:");
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
double delta = Math.sqrt(b * b - 4 * a * c);
if (delta < 0) {
System.out.println("The ecuation has no real roots.");
} else if (delta == 0) {
double root = -b / (2 * a);
System.out.println("The ecuation has one root " + root);
} else if (delta > 0) {
double root1 = (-b + delta) / (2 * a);
double root2 = (-b - delta) / (2 * a);
System.out.println("The ecuation has two roots.");
System.out.println("Root1: " + root1 + " Root2: " + root
2);
}
input.close();
}
static void problema2() {
/*
* (Random month) Write a program that randomly generates an int
eger
* between 1 and 12 and displays the English month name January,
* February, , December for the number 1, 2, , 12, accordingly.3.5
*/
int monthId = (int) (Math.random() * 12) + 1;
String x;
switch (monthId) {
case 1:
x = "Ianuarie";
break;
case 2:
x = "Februarie";
break;
case 3:
x = "Martie";
break;
case 4:
x = "Aprilie";
break;
case 5:
x = "Mai";
break;
case 6:
x = "Iunie";
break;
case 7:
x = "Iulie";
break;
case 8:
x = "August";
break;
case 9:
x = "Septembrie";
break;
case 10:
x = "Octombrie";
break;
case 11:
x = "Noiembrie";
break;
case 12:
x = "Decembrie";
break;
default:
x = "Some strange error ocured.";
}
System.out.println(x);
}
static void problema3() {
/*
* (Find future dates) Write a program that prompts the user to
enter an
* integer for todays day of the week (Sunday is 0, Monday is 1, ,
and
* Saturday is 6). Also prompt the user to enter the number of d
ays
* after today for a future day and display the future day of th
e week.
* Here is a sample run:
*/
Scanner input = new Scanner(System.in);
System.out.println("Enter today's day:");
int today = input.nextInt();
System.out.println("Enter the number of days elapsed since today
:");
int elapsed = input.nextInt();
int logic = (today + elapsed) % 7;
System.out.println(logic);
String day;
switch (logic) {
case 0:
day = "Duminica";
break;
case 1:
day = "Luni";
break;
case 2:
day = "Marti";
break;
case 3:
day = "Miercuri";
break;
case 4:
day = "Joi";
break;
case 5:
day = "Vineri";
break;
case 6:
day = "Sambata";
break;
default:
day = "Locical Error Occured";
}
System.out.println(day);
input.close();
}
static void problema4() {
/*
* (Sort three integers) Write a program that prompts the user t
o enter
* three integers and display the integers in non-decreasing ord
er.
*/
Scanner input = new Scanner(System.in);
System.out.println("Enter three integers: ");
int a = input.nextInt();
int b = input.nextInt();
int c = input.nextInt();
int[] numbers = new int[] { a, b, c };
Arrays.sort(numbers);
System.out.println("The numbers are: " + Arrays.toString(numbers
));
input.close();
}
static void problema5() {
/*
* (Business: check ISBN-10) An ISBN-10(International Standard B
ook
* Number) consists of 10 digits: d1d2d3d4d5d6d7d8d9d10 . The la
st
* digit, d10 , is a checksum, which is calculated from the othe
r nine
* digits using the following formula: (d1 *1+d2 *2+d3 *3+d4 *4+
d5 *5+
* d6 *6+d7 *7+d8 *8+d9 *9) % 11 If the checksum is 10, the last
digit
* is denoted as X according to the ISBN-10 convention. Write a
program
* that prompts the user to enter the first 9 digits and display
s the
* 10-digit ISBN (including leading zeros). Your program should
read the
* input as an integer. Here are sample runs:
*/
System.out.println("Enter the first 9 digits of an ISBN as integ
er:");
int d1, d2, d3, d4, d5, d6, d7, d8, d9, d10;
Scanner input = new Scanner(System.in);
int nums = input.nextInt();
int[] temp = new int[8];
int len = (int) Math.log10(nums) + 1;
for (int i = 0; i < len; i++) {
temp[i] = nums % 10;
nums /= 10;
}
d1 = 0;
d2 = temp[7];
d3 = temp[6];
d4 = temp[5];
d5 = temp[4];
d6 = temp[3];
d7 = temp[2];
d8 = temp[1];
d9 = temp[0];
temp = null;
d10 = (d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5 + d6 * 6 + d7
* 7
+ d8 * 8 + d9 * 9) % 11;
if (d10 == 10) {
System.out.print("The ISBN-10 number is ");
System.out.println(d1 + "" + d2 + d3 + d4 + d5 + d6 + d7
+ d8 + d9
+ "X");
} else {
System.out.print("The ISBN-10 number is ");
System.out.println(d1 + "" + d2 + d3 + d4 + d5 + d6 + d7
+ d8 + d9
+ d10);
}
input.close();
}
static void problema6() {
/*
* (Find the number of days in a month) Write a program that pro
mpts the
* user to enter the month and year and displays the number of d
ays in
* the month. For example, if the user entered month 2and year 2
012, the
* program should display that February 2012 had 29 days. If the
user
* entered month 3and year 2015, the program should display that
March
* 2015 had 31 days.
*/
Scanner input = new Scanner(System.in);
System.out.println("Enter a month number ex Januarry = 1: ");
int month = input.nextInt();
System.out.println("Enter a year: ");
int year = input.nextInt();
String monthName = null;
int monthDays = 0;
boolean leapYear = (year % 4 == 0 && year % 100 != 0)
|| (year % 400 == 0);
if (leapYear && month == 2) {
monthName = "February";
monthDays = 29;
} else {
switch (month) {
case 1:
monthName = "January";
monthDays = 31;
break;
case 2:
monthName = "February";
monthDays = 28;
break;
case 3:
monthName = "March";
monthDays = 31;
break;
case 4:
monthName = "April";
monthDays = 30;
break;
case 5:
monthName = "May";
monthDays = 31;
break;
case 6:
monthName = "June";
monthDays = 30;
break;
case 7:
monthName = "July";
monthDays = 31;
break;
case 8:
monthName = "August";
monthDays = 31;
break;
case 9:
monthName = "September";
monthDays = 30;
break;
case 10:
monthName = "Octomber";
monthDays = 31;
break;
case 11:
monthName = "November";
monthDays = 30;
break;
case 12:
monthName = "December";
monthDays = 31;
break;
}
}
System.out.println(monthName + " " + year + " had " + monthDays
+ " days.");
input.close();
}
static void problema7() {
/*
* (Game: lottery) Revise Listing 3.8, Lottery.java, to generate
a
* lottery of a threedigit number. The program prompts the user
to enter
* a three-digit number and determines whether the user wins acc
ording
* to the following rules: 1. If the user input matches the lott
ery
* number in the exact order, the award is $10,000. 2. If all di
gits in
* the user input match all digits in the lottery number, the aw
ard is
* $3,000. 3. If one digit in the user input matches a digit in
the
* lottery number, the award is $1,000.
*/
Scanner input = new Scanner(System.in);
System.out.println("Enter a 3 digit number: ");
int loteryNumber = (int) Math.random() * 900 + 100;
int loteryNumberU = loteryNumber;
int loteryGuess = input.nextInt();
int loteryNumberLen = (int) Math.log10(loteryGuess) + 1;
int digit3 = loteryGuess % 10;
loteryGuess /= 10;
int digit2 = loteryGuess % 10;
int digit1 = loteryGuess / 10;
int lotery3 = loteryNumber % 10;
loteryNumber /= 10;
int lotery2 = loteryNumber % 10;
int lotery1 = loteryNumber / 10;
if ((loteryNumberLen > 0) && (loteryNumberLen <= 3)) {
if (loteryGuess == loteryNumberU) {
System.out.println("You won 10.000$");
} else if ((digit1 == lotery1 || digit1 == lotery2 || di
git1 == lotery3)
&& (digit2 == lotery1 || digit2 == loter
y2 || digit2 == lotery3)
&& (digit3 == lotery1 || digit3 == loter
y2 || digit3 == lotery3)) {
System.out.println("You won 3.000$");
} else if (digit1 == lotery1 || digit2 == lotery2
|| digit1 == lotery3) {
System.out.println("You won 1.000$");
} else if (digit2 == lotery1 || digit2 == lotery2
|| digit2 == lotery3) {
System.out.println("You won 1.000$");
} else if (digit3 == lotery1 || digit3 == lotery2
|| digit3 == lotery3) {
System.out.println("You won 1.000$");
}
} else {
System.out.println("Wrong input.");
}
input.close();
}
static void problema8() {
/*
* (Game: scissor, rock, paper) Write a program that plays the p
opular
* scissor-rockpaper game. (A scissor can cut a paper, a rock ca
n knock
* a scissor, and a paper can wrap a rock.) The program randomly
* generates a number 0, 1, or 2representing scissor, rock, and
paper.
* The program prompts the user to enter a number 0,1, or 2and d
isplays
* a message indicating whether the user or the computer wins, l
oses, or
* draws. Here are sample runs:
*/
Scanner input = new Scanner(System.in);
int computer = (int) (Math.random() * 3);
System.out.println("scissor (0), rock (1), paper (2):");
int human = input.nextInt();
String choice;
String choiceH;
switch (computer) {
case 0:
choice = "scissor";
break;
case 1:
choice = "rock";
break;
case 2:
choice = "paper";
break;
default:
choice = "Error";
}
switch (human) {
case 0:
choiceH = "scissor";
break;
case 1:
choiceH = "rock";
break;
case 2:
choiceH = "paper";
break;
default:
choiceH = "Error";
}
String win = null;
if (human == 0) {
if (computer == 1) {
win = "false";
} else if (computer == 2) {
win = "true";
} else {
win = "draw";
}
} else if (human == 1) {
if (computer == 0) {
win = "true";
} else if (computer == 2) {
win = "false";
} else {
win = "draw";
}
} else if (human == 2) {
if (computer == 1) {
win = "true";
} else if (computer == 0) {
win = "false";
} else {
win = "draw";
}
}
if (win.equals("true")) {
System.out.println("The computer is " + choice + " You a
re "
+ choiceH + ". You won.");
} else if (win.equals("false")) {
System.out.println("The computer is " + choice + " You a
re "
+ choiceH + ". You lose.");
} else {
System.out.println("The computer is " + choice + " You a
re "
+ choiceH + ". It's a draw.");
}
input.close();
}
static void problema9() {
/*
* (Game: pick a card) Write a program that simulates picking a
card
* from a deck of52cards. Your program should display the rank
* (Ace,2,3,4,5,6,7,8,9,10, Jack,Queen,King) and suit
* (Clubs,Diamonds,Hearts,Spades) of the card. Here is a sample
run of
* the program:
*/
Scanner input = new Scanner(System.in);
System.out.println("Pick a card, any card i.e J (jack): ");
String card = input.nextLine();
String suit;
int randomCard = (int) (Math.random() * 4);
switch (randomCard) {
case 0:
suit = "Clubs";
break;
case 1:
suit = "Diamonds";
break;
case 2:
suit = "Hearts";
break;
case 3:
suit = "Spades";
break;
default:
suit = "Error";
}
String regex = "\\d+";
if (card.toLowerCase().contains("j")) {
System.out.println("The card you picked is Jack of " + s
uit);
} else if (card.toLowerCase().contains("q")) {
System.out.println("The card you picked is Quuen of " +
suit);
} else if (card.toLowerCase().contains("k")) {
System.out.println("The card you picked is King of " + s
uit);
} else if (card.toLowerCase().contains("a")) {
System.out.println("The card you picked is Ace of " + su
it);
} else if (card.matches(regex)) {
int cardInt = Integer.parseInt(card);
if (cardInt >= 2 && cardInt <= 10) {
System.out.println("The card you picked is " + c
ard + " of "
+ suit);
} else {
System.out.println("Wrong Input!");
}
} else {
System.out.println("Wrong Input!");
}
input.close();
}
static void problema10() {
/*
* (Financials: currency exchange) Write a program that prompts
the user
* to enter the exchange rate from currency in U.S. dollars to C
hinese
* RMB. Prompt the user to enter 0to convert from U.S. dollars t
o
* Chinese RMB and 1to convert from Chinese RMB and U.S. dollars
. Prompt
* the user to enter the amount in U.S. dollars or Chinese RMB t
o
* convert it to Chinese RMB or U.S. dollars, respectively. Here
are the
* sample runs:
*/
Scanner input = new Scanner(System.in);
System.out.println("Enter the exchange rate from dollars to RMB:
");
final double EXCHANGERATE = input.nextDouble();
System.out.println("Enter 0 to convert dollars to RMB and 1 vice
versa:");
int select = input.nextInt();
if (select == 0) {
System.out.println("Enter the dollar amount:");
double amount = input.nextDouble();
double convert = amount * EXCHANGERATE;
System.out.println("$" + amount + " is " + convert + " y
uan.");
} else if (select >= 1) {
System.out.println("Enter the RMB amount:");
double amount = input.nextDouble();
double convert = amount / EXCHANGERATE;
System.out.println(amount + " yuan is $" + convert);
}
input.close();
}
}

You might also like