You are on page 1of 8

/*

* 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.Scanner;
public class SetProbleme4 {
public static void main(String[] args) {
// problema1();
// problema2();
// problema3();
// problema4();
// problema5();
// problema6();
// problema7();
// problema8();
//problema9();
problema10();
}
static void problema1() {
/*
* (Geometry: great circle distance) The great circle distance i
s the
* distance between two points on the surface of a sphere. Let (
x1,y1)
* and (x2,y2) be the geographical latitude and longitude of two
points.
* The great circle distance between the two points can be compu
ted
* using the following formula: d=radius *arccos(sin(x1 ) *sin(x
2 )
* +cos(x1 ) *cos(x2 ) *cos(y1 -y2 )) Write a program that promp
ts the
* user to enter the latitude and longitude of two points on the
earth
* in degrees and displays its great circle distance. The averag
e earth
* radius is 6,371.01 km. Note that you need to convert the degr
ees into
* radians using the Math.toRadiansmethod since the Java trigono
metric
* methods use radians. The latitude and longitude degrees in th
e
* formula are for north and west. Use negative to indicate sout
h and
* east degrees.
*/
final double RADIUS = 6.371;
Scanner input = new Scanner(System.in);
System.out
.println("Enter point 1 (latitude and longitude)
in degrees: ex 2.4, 3");
String line1 = input.nextLine();
System.out
.println("Enter point 2 (latitude and longitude)
in degrees:");
String line2 = input.nextLine();
double point1x = Double.parseDouble(line1.split(",")[0]);
double point1y = Double.parseDouble(line1.split(",")[1]);
double point2x = Double.parseDouble(line2.split(",")[0]);
double point2y = Double.parseDouble(line2.split(",")[1]);
double x1 = Math.toRadians(point1x);
double x2 = Math.toRadians(point2x);
double y1 = Math.toRadians(point1y);
double y2 = Math.toRadians(point2y);
double d = RADIUS
* Math.acos(Math.sin(x1) * Math.sin(x2) + Math.c
os(x1)
* Math.cos(x2) * Math.cos(y1 - y
2));
System.out.println("The distance between the two points is " + d
+ " km.");
input.close();
}
static void problema2() {
/*
* Geometry: area of a regular polygon) A regular polygon is an
n-sided
* polygon in which all sides are of the same length and all ang
les have
* the same degree (i.e., the polygon is both equilateral and
* equiangular). Write a program that prompts the user to enter
the
* number of sides and their length of a regular polygon and dis
plays
* its area. Here is a sample run:
*/
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of sides:");
int numberOfSides = input.nextInt();
System.out.println("Enter the side:");
double side = input.nextDouble();
double area = (numberOfSides * side * side)
/ (4 * Math.tan(Math.PI / numberOfSides));
System.out.println("The area of the polygon is " + area);
input.close();
}
static void problema3() {
/*
* (Find the character of an ASCII code) Write a program that re
ceives
* an ASCII code (an integer between 0and 127) and displays its
* character.
*/
Scanner input = new Scanner(System.in);
System.out.println("Enter an ASCII code:");
int asciiChar = input.nextInt();
System.out.println("The character for ASCII code " + asciiChar +
" is "
+ (char) asciiChar);
input.close();
}
static void problema4() {
/*
* (Find the Unicode of a character) Write a program that receiv
es a
* character and displays its Unicode. Here is a sample run:
*/
Scanner input = new Scanner(System.in);
System.out.println("Enter a character:");
char asciiChar = input.next().charAt(0);
System.out.println("The Unicode for the character " + asciiChar
+ " is " + (int) asciiChar);
input.close();
}
static void problema5() {
/*
* (Decimal to hex) Write a program that prompts the user to ent
er an
* integer between 0and 15and displays its corresponding hex num
ber.
* Here are some sample runs:
*/
Scanner input = new Scanner(System.in);
System.out.println("Enter a decimal value (0 to 15):");
int hexChar = input.nextInt();
if (hexChar >= 10 && hexChar <= 15) {
String character = Integer.toHexString(hexChar).toUpperC
ase();
System.out.println("The hex value is " + character);
} else if (hexChar > 0 && hexChar < 10) {
System.out.println("The hex value is " + hexChar);
} else {
System.out.println(hexChar + " is an invalid input.");
}
input.close();
}
static void problema6() {
/*
* (Vowel or consonant?) Write a program that prompts the user t
o enter
* a letter and check whether the letter is a vowel or consonant
.
*/
Scanner input = new Scanner(System.in);
System.out.println("Enter a letter:");
char check = input.next().charAt(0);
boolean isVowel = "AEIOUaeiou".indexOf(check) != -1;
if (isVowel) {
System.out.println(check + " is a vowel.");
} else if (check >= 65 && check <= 90) {
System.out.println(check + " is a consonant.");
} else if (check >= 97 && check <= 122) {
System.out.println(check + " is a consonant.");
} else {
System.out.println(check + " is an invalid input.");
}
input.close();
}
static void problema7() {
/*
* (Phone key pads) The international standard letter/number map
ping
* found on the telephone is shown below: Write a program that p
rompts
* the user to enter a letter and displays its correspond- ing n
umber.
*/
Scanner input = new Scanner(System.in);
System.out.println("Enter a letter: ");
char letter = input.next().charAt(0);
if (letter >= 65 && letter <= 90) {
boolean found = "ABCDEFGHIJKLMNOPQR".indexOf(letter) !=
-1;
int number = 1;
if (found) {
number = ("ABCDEFGHIJKLMNOPQR".indexOf(letter) /
3) + 2;
} else {
number = ("STUVWXYZ").indexOf(letter);
switch (number) {
case 0:
number = 7;
break;
case 1:
number = 8;
break;
case 2:
number = 8;
break;
case 3:
number = 8;
break;
case 4:
number = 9;
break;
case 5:
number = 9;
break;
case 6:
number = 9;
break;
case 7:
number = 9;
break;
}
}
System.out.println("The corresponding number is " + numb
er);
} else if (letter >= 97 && letter <= 122) {
boolean found = "abcdefghijklmnopqr".indexOf(letter) !=
-1;
int number = 1;
if (found) {
number = ("abcdefghijklmnopqr".indexOf(letter) /
3) + 2;
} else {
number = ("stuvwxyz").indexOf(letter);
switch (number) {
case 0:
number = 7;
break;
case 1:
number = 8;
break;
case 2:
number = 8;
break;
case 3:
number = 8;
break;
case 4:
number = 9;
break;
case 5:
number = 9;
break;
case 6:
number = 9;
break;
case 7:
number = 9;
break;
}
}
System.out.println("The corresponding number is " + numb
er);
} else {
System.out.println(letter + " is an invalid input.");
}
input.close();
}
static void problema8() {
/*
* (Days of a month) Write a program that prompts the user to en
ter a
* year and the first three letters of a month name (with the fi
rst
* letter in uppercase) and displays the number of days in the m
onth.
*/
Scanner input = new Scanner(System.in);
System.out.println("Enter a year:");
String yearName = input.nextLine();
int year = Integer.parseInt(yearName);
System.out.println("Enter a month:");
String monthName = input.nextLine();
int monthDays = 0;
String month = monthName.substring(0, 1).toUpperCase()
+ monthName.substring(1, 3);
boolean leapYear = (year % 4 == 0 && year % 100 != 0)
|| year % 400 == 0;
if (leapYear && month.equals("Feb")) {
monthName = "February";
monthDays = 29;
} else {
switch (month) {
case "Jan":
monthName = "January";
monthDays = 31;
break;
case "Feb":
monthName = "February";
monthDays = 28;
break;
case "Mar":
monthName = "March";
monthDays = 31;
break;
case "Apr":
monthName = "April";
monthDays = 30;
break;
case "May":
monthName = "May";
monthDays = 31;
break;
case "Jun":
monthName = "June";
monthDays = 30;
break;
case "Jul":
monthName = "July";
monthDays = 31;
break;
case "Aug":
monthName = "August";
monthDays = 31;
break;
case "Sep":
monthName = "September";
monthDays = 30;
break;
case "Oct":
monthName = "Octomber";
monthDays = 31;
break;
case "Nov":
monthName = "November";
monthDays = 30;
break;
case "Dec":
monthName = "December";
monthDays = 31;
break;
}
}
System.out.println(monthName + " " + year + " had " + monthDays
+ " days.");
input.close();
}
static void problema9() {
/*
* (Student major and status) Write a program that prompts the u
ser to
* enter two characters and displays the major and status repres
ented in
* the characters. The first character indicates the major and t
he
* second is number character 1, 2, 3, 4, which indicates whethe
r a
* student is a freshman, sophomore, junior, or senior. Suppose
the
* following chracters are used to denote the majors: M:Mathemat
ics
* C:Computer Science I:Information Technology Here is a sample
run:
*/
Scanner input = new Scanner(System.in);
System.out.println("Enter two characters:");
String line = input.next().toUpperCase();
String materie = line.substring(0, 1);
String numeMaterie = "";
boolean legit1 = false;
boolean legit2 = false;
switch (materie) {
case "M":
legit1 = true;
numeMaterie = "Mathematics";
break;
case "C":
legit1 = true;
numeMaterie = "Computer Science";
break;
case "I":
legit1 = true;
numeMaterie = "Information Technology";
break;
}
String grad = line.substring(1, 2);
String numeGrad = "";
switch (grad) {
case "1":
legit2 = true;
numeGrad = "freshman";
break;
case "2":
legit2 = true;
numeGrad = "sophomore";
break;
case "3":
legit2 = true;
numeGrad = "junior";
break;
case "4":
legit2 = true;
numeGrad = "senior";
break;
}
if(legit1&&legit2) {
System.out.println(numeMaterie + " " + numeGrad);
} else {
System.out.println(line + " is an invalid input.");
}
input.close();
}
static void problema10() {
/*
* (Check SSN) Write a program that prompts the user to enter a
Social
* Security number in the format DDD-DD-DDDD, where D is a digit
. Your
* program should check whether the input is valid. Here are sam
ple
* runs:
*/
Scanner input = new Scanner(System.in);
System.out.println("Enter a SSN:");
String ssn = input.nextLine();
if (ssn.length() < 11) {
System.out.println(ssn + " is an invalid social security
number.");
} else {
String regex = "\\d+";
String part1 = ssn.substring(0,3);
String part2 = ssn.substring(4,6);
String part3 = ssn.substring(7);
if (part1.matches(regex)&&part2.matches(regex)&&part3.ma
tches(regex)) {
System.out.println(ssn + " is a valid social sec
urity number.");
} else {
System.out.println(ssn + " is an invalid social
security number.");
}
}
input.close();
}
}

You might also like