In Java, in the same program write code for the following... A. Generate 2 rando
ID: 3903435 • Letter: I
Question
In Java, in the same program write code for the following...
A. Generate 2 random integers to represent the throw of 2 dice (1 through 6). Each random number is between 1 and 6. Calculate the total of the 2 numbers. Display the total and the message “Oh crap!” if the total is 2, 3, or 12. Display the total and the message “All natural” If the total is 7 or 11. Display the total and the message “OK points” if the total is 4, 5, 6, 8, 9, 10.
B. Ask the user to enter a letter and check whether the letter is upper case, lower case, a digit, a vowel (a, e, i, o, u). When checking for a vowel, case should not matter. So, if the user enters A, it would still be counted as a vowel. Display appropriate messages based on the given letter. Display the letter in both upper case and lower case, regardless of how it was entered. Use a built-in function to convert the input to its ASCII numeric equivalent and display the result. For example, ASCII equivalent of A is 65.
C. Ask the user for a string. If the input string is entered exactly as “myjava”, display an error message and do not perform any operation. Display the length of the string and the first 3 characters of the string, only if the string has 3 characters or more. Otherwise, display an error message and don’t process the input data. Display the string in upper case, lower case, and display the first and the last character of the string. Create and display the value of a new string that holds the first 2 characters of the string followed by “,.” (comma followed by a period) followed by the last 3 characters of the string. Of course, if the string does not have the required number of characters, issue an error message. For example:
Test case 1: if the string is entered as “hello world”, we will display:
11 <<<< the length of the string
hel
HELLO WORLD
hello world
h
d
he,.rld
Explanation / Answer
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
import java.util.Random;
import java.util.Scanner;
public class Test {
public static void main(String[] args)
{
taskA();
taskB();
taskC();
}
//throw of 2 dice
private static void taskA()
{
System.out.println("Throwing of 2 dice");
Random rand = new Random();
int d1 = rand.nextInt(6) + 1;
int d2 = rand.nextInt(6) + 1;
int total = d1 + d2;
System.out.println("Dice: " + d1 + ", " + d2 );
System.out.println("Total: " + total);
switch(total)
{
case 2:
case 3:
case 12:
System.out.println("Oh Crap!");
break;
case 7:
case 11:
System.out.println("All natural");
break;
case 4:
case 5:
case 6:
case 8:
case 9:
case 10:
System.out.println("OK Points");
break;
}
System.out.println("-------------------");
}
private static void taskB()
{
char ch;
Scanner keyboard = new Scanner(System.in);
System.out.println("Check whether a character is lower case, uppper case , ");
System.out.println("Enter a character: ");
ch = keyboard.next().charAt(0);
System.out.println("You entered: " + ch);
if((ch >= 'a' && ch <= 'z'))
{
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
System.out.println("You entered a vowel");
else
System.out.println("You entered a lower case letter");
System.out.println("Lower case: " + Character.toLowerCase(ch));
System.out.println("Upper case: " + Character.toUpperCase(ch));
}
else if((ch >= 'A' && ch <= 'Z'))
{
if(ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
System.out.println("You entered a vowel");
else
System.out.println("You entered an upper case letter");
System.out.println("Lower case: " + Character.toLowerCase(ch));
System.out.println("Upper case: " + Character.toUpperCase(ch));
}
else if(ch >= '0' && ch <= '9')
{
System.out.println("You entered a digit");
}
int ascii = ch;
System.out.println("ASCII Value: " + ascii);
System.out.println("-------------------");
}
private static void taskC()
{
String s;
Scanner keyboard = new Scanner(System.in);
System.out.println("String processing");
System.out.println("Enter a string: ");
s = keyboard.nextLine();
if(s.equals("myjava"))
System.out.println("ERROR!");
else
{
int len = s.length();
System.out.println(len);
if(len >= 3)
{
System.out.println(s.substring(0, 3)); //first 3 chars
System.out.println(s.toUpperCase()); //upper case
System.out.println(s.toLowerCase()); //lower case
System.out.println(s.charAt(0)); //fisrt char
System.out.println(s.charAt(len-1)); //last char
String s1 = s.substring(0, 2) + ",." + s.substring(len - 3, len);
System.out.println(s1);
}
else
{
System.out.println("ERROR: length of string is less than 3");
}
}
}
}
output
===
Throwing of 2 dice
Dice: 5, 1
Total: 6
OK Points
-------------------
Check whether a character is lower case, uppper case ,
Enter a character:
A
You entered: A
You entered a vowel
Lower case: a
Upper case: A
ASCII Value: 65
-------------------
String processing
Enter a string:
hello world
11
hel
HELLO WORLD
hello world
h
d
he,.rld
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.