Part #2: Using Math.random() with a deck of cards Write a program using Math.ran
ID: 3879405 • Letter: P
Question
Part #2: Using Math.random() with a deck of cards Write a program using Math.random) to randomly pick a card from a deck. Hint: You will need to use Math.random) twice: once for the suit and once for the rank. Print the result (e.g. "Ace of Spades"). Part #3: Calendar using switch statement Write a program that inputs a number from 1 to 12 from the user and then prints the corresponding month and it's number of days (e.g. input of 5 would yield "May has 31 days"). Use the Switch statement Part #4: Simple loop Write a program to print the numbers from 1 to 20 along with their squares. Use the FOR loop construct Part #5: Index Write a program to ask the user for a string and then find the first occurrence of a vowel. You will output the index position. For example, an input string of "hello would give index 1 (second letter).Explanation / Answer
Part #2:
class Chegg1{
public static void main(String[] args){
// make the cards
int[] deck = new int[52]; // how many total
String[] suits = {"Hearts", "Diamonds", "Spades", "Clubs"};
String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
// intitialize cards
for (int i = 0; i < deck.length; i++){
deck[i] += i;
}
// shuffle cards
for (int i = 0; i < deck.length; i++){
int index = (int)(Math.random() * deck.length);
int x = deck[i]; // x now has all 52 cards?
deck[i] = deck[index]; // pick a random card
deck[index] = x; // pick a radom card
}
// display four cards
for (int i = 0; i < 4; i++){
String suit = suits[deck[i] / 13];
String rank = ranks[deck[i] % 13];
System.out.println("You have the " + rank + " of " + suit);
}
}
}
Part #3:
import java.util.Scanner;
class Chegg2 {
public static void main(String[] strings) {
Scanner input = new Scanner(System.in);
int number_Of_DaysInMonth = 0;
String MonthOfName = "Unknown";
System.out.print("Input a month number: ");
int month = input.nextInt();
System.out.print("Input a year: ");
int year = input.nextInt();
switch (month) {
case 1:
MonthOfName = "January";
number_Of_DaysInMonth = 31;
break;
case 2:
MonthOfName = "February";
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
number_Of_DaysInMonth = 29;
} else {
number_Of_DaysInMonth = 28;
}
break;
case 3:
MonthOfName = "March";
number_Of_DaysInMonth = 31;
break;
case 4:
MonthOfName = "April";
number_Of_DaysInMonth = 30;
break;
case 5:
MonthOfName = "May";
number_Of_DaysInMonth = 31;
break;
case 6:
MonthOfName = "June";
number_Of_DaysInMonth = 30;
break;
case 7:
MonthOfName = "July";
number_Of_DaysInMonth = 31;
break;
case 8:
MonthOfName = "August";
number_Of_DaysInMonth = 31;
break;
case 9:
MonthOfName = "September";
number_Of_DaysInMonth = 30;
break;
case 10:
MonthOfName = "October";
number_Of_DaysInMonth = 31;
break;
case 11:
MonthOfName = "November";
number_Of_DaysInMonth = 30;
break;
case 12:
MonthOfName = "December";
number_Of_DaysInMonth = 31;
}
System.out.print(MonthOfName + " " + year + " has " + number_Of_DaysInMonth + " days ");
}
}
Part #4:
import java.lang.*;
class Chegg3
{
public static void main (String [] args)
{
double number;
int square;
System.out.println("Number Square");
for (number=1; number<=20; number++)
{
square=(int)Math.pow(number,2);
System.out.println((int)number+" "+(int)square);
}
}
}
Part #5:
import java.util.*;
class Chegg4
{
static String vowels = "aeiou";
public static int indexOfFirstVowel(String word){
String loweredWord = word.toLowerCase();
for (int index = 0; index < loweredWord.length(); index++)
{
if (vowels.contains(String.valueOf(loweredWord.charAt(index))))
{
System.out.println("vowel found at"+index);
return index;
}
}
// handle cases where a vowel is not found
return -1;
}
public static void main(String... s)
{
Scanner sc =new Scanner(System.in);
String ss=sc.nextLine();
//Chegg4 obj=new Chegg4();
indexOfFirstVowel(ss);
}}