Introduction to Java Programming Chapter 4 Revel Questions

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

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.

import java.util.Scanner; public class Exercise04_21 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a SSN: "); String ssn = input.nextLine(); boolean isValid = ssn.length() == 11 && ssn.charAt(0) <= '9' && ssn.charAt(0) >= '0' && Character.isDigit(ssn.charAt(1)) && Character.isDigit(ssn.charAt(2)) && ssn.charAt(3) == '-' && Character.isDigit(ssn.charAt(4)) && Character.isDigit(ssn.charAt(5)) && ssn.charAt(6) == '-' && Character.isDigit(ssn.charAt(7)) && Character.isDigit(ssn.charAt(8)) && Character.isDigit(ssn.charAt(9)) && Character.isDigit(ssn.charAt(10)); if (isValid) System.out.println(ssn + " is a valid social security number"); else System.out.println(ssn + " is an invalid social security number"); } }

Write a program that prompts the user to enter the number of sides and their length of a regular polygon and displays its area.

import java.util.Scanner; public class Exercise04_05 { public static void main(String[] args) { Scanner input = new Scanner(System.in); // Prompt the user to enter the number of sides // and their length of a regular polygon System.out.print("Enter the number of sides: "); int numberOfSides = input.nextInt(); System.out.print("Enter the side: "); double side = input.nextDouble(); // Compute the area of a regular polygon double area = (numberOfSides * Math.pow(side, 2) / (4 * Math.tan(Math.PI / numberOfSides))); // Display result System.out.println("The area of the polygon is " + area); }}

Write a program that receives an ASCII code (an integer between 0 and 127) and displays its character.

import java.util.Scanner; public class Exercise04_08 { public static void main(String[] args) { Scanner input = new Scanner(System.in); // Prompt the user to enter an ASCII code // (an integer between 0 and 127) System.out.print("Enter an ASCII code: "); int i = input.nextInt(); // Display ASCII code as character System.out.println((char)i); }}

Write a program that prompts the user to enter an integer between 0 and 15 and displays its corresponding hex number. For an incorrect input number, display invalid input. Here is a sample run:

import java.util.Scanner; public class Exercise04_11 { public static void main(String[] args) { Scanner input = new Scanner(System.in); // Prompt the user to enter an integer between 0 and 15 System.out.print("Enter a decimal value (0 to 15): "); int decimal = input.nextInt(); // Display its corresponding hex number if (decimal >= 0 && decimal <= 9) System.out.println("The hex value is " + decimal); else if (decimal >= 10 && decimal <= 15) System.out.println("The hex value is " + (char)(decimal + 'A' - 10)); else System.out.println(decimal + " is an invalid input"); }}

Write a program that prompts the user to enter a letter grade A, B, C, D, or F and displays its corresponding numeric value 4, 3, 2, 1, or 0. For other input, display invalid grade.

import java.util.Scanner; public class Exercise04_14 { public static void main(String[] args) { Scanner input = new Scanner(System.in); // Prompt the user to enter a letter grade A, B, C, D or F System.out.print("Enter a letter grade: "); String s = input.nextLine(); char ch = s.charAt(0); ch = Character.toUpperCase(ch); if (ch >= 'A' && ch <= 'F' && ch != 'E') { System.out.print("The numeric value for grade " + ch + " is "); switch(ch) { case 'A': System.out.println(4); break; case 'B': System.out.println(3); break; case 'C': System.out.println(2); break; case 'D': System.out.println(1); break; case 'F': System.out.println(0); } } else System.out.println(ch + " is an invalid grade"); }}

Write a program that prompts the user to enter a year and the first three letters of a month name (with the first letter in uppercase) and displays the number of days in the month.

import java.util.Scanner; public class Exercise04_17 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a year: "); int year = input.nextInt(); System.out.print("Enter a month (first three letters with the first letter in uppercase): "); String month = input.next(); if (month.equals("Jan") || month.equals("Mar") || month.equals("May") || month.equals("Jul") || month.equals("Aug") || month.equals("Oct") || month.equals("Dec")) System.out.print(month + " " + year + " has 31 days"); else if (month.equals("Apr") || month.equals("Jun") || month.equals("Sep") || month.equals("Nov")) System.out.print(month + " " + year + " has 30 days"); else if (month.equals("Feb")) if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) System.out.print(month + " " + year + " has 29 days"); else System.out.print(month + " " + year + " has 28 days"); else System.out.print(month + " is not a correct month name "); } }

Write a program that displays a random uppercase letter using the Math.random() method.

import java.util.Scanner; //Write a program that displays a random upper-case // letter using the Math.random() method. public class Exercise04_16 { public static void main(String[] args) { //Random integer from 0 - 26 int randomNumber = (int)(Math.random() * 27); String alphabet = "abcdefghijklmnopqrstuvwxyz"; // select a random character out of the alphabet and // display print upper-case System.out.println(Character.toUpperCase(alphabet.charAt(randomNumber))); } }

A regular polygon is an n-sided polygon in which all sides are of the same length and all angles have the same degree (i.e., the polygon is both equilateral and equiangular). The formula for computing the area of a regular polygon is. Write a program that prompts the user to enter the number of sides and their length, and displays its area.

import java.util.Scanner; public class Exercise04_05 { public static void main(String[] args) { Scanner input = new Scanner(System.in); // Enter the number of sides System.out.print("Enter the number of sides: "); int numberOfSides = input.nextInt(); System.out.print("Enter the side: "); double side = input.nextDouble(); double area = numberOfSides * side * side / Math.tan(Math.PI / numberOfSides) / 4; System.out.println("The area of the polygon is " + area); } }

Write a program that prompts the user to enter an int between 0 and 15 and displays its corresponding hex number. For an incorrect input number, display invalid input.

import java.util.Scanner; public class Exercise04_11 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a decimal value (0 to 15): "); int decimal = input.nextInt(); if (decimal > 15 || decimal < 0) System.out.println(decimal + " is an invalid input"); else if (decimal < 10) System.out.println("The hex value is " + decimal); else System.out.println("The hex value is " + (char)('A' + decimal - 10)); } }

Write a program that prompts the user to enter a letter and check whether the letter is a vowel or consonant. For a nonletter input, display invalid input.

import java.util.Scanner; public class Exercise04_13 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a letter: "); char letter = input.nextLine().charAt(0); if (Character.toUpperCase(letter) == 'A' || Character.toUpperCase(letter) == 'E' || Character.toUpperCase(letter) == 'I' || Character.toUpperCase(letter) == 'O' || Character.toUpperCase(letter) == 'U') System.out.println(letter + " is a vowel"); else if (Character.isLetter(letter)) System.out.println(letter + " is a consonant"); else System.out.println(letter + " is an invalid input"); } }

Write a program that prompts the user to enter two characters and displays the major and status represented in the characters. The first character indicates the major and the second is number character 1, 2, 3, 4, which indicates whether a student is a freshman, sophomore, junior, or senior. Suppose the following characters are used to denote the majors: M: Mathematics C: Computer Science I: Information Technology

import java.util.Scanner; public class Exercise04_18{ public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter two characters: "); String status = in.next(); char major = Character.toUpperCase(status.charAt(0)); char year = status.charAt(1); String courseName = ""; String yearName = ""; if (major == 'M' || major == 'I' || major == 'C') { switch(major) { case 'M': courseName = "Mathematics"; break; case 'C': courseName = "Computer Science"; break; case 'I': courseName = "Information Technology"; break; default: break; } switch(year) { case '1': yearName = "Freshman"; break; case '2': yearName = "Sophmore"; break; case '3': yearName = "Junior"; break; case '4': yearName = "Senior"; break; default: break; } System.out.printf("%s %s%n", courseName, yearName); } else{ System.out.printf("Invalid input.%n"); } }}


Kaugnay na mga set ng pag-aaral

MKTG 3301 Assignment 2 (missing Gr .question)

View Set

CCNP - Network Security Controls Post Assessment

View Set

Foundations of Nursing - Chapter 10 Test

View Set

Psychology of Emotion | Midterm 2

View Set

Chapter 16: Drug Therapy to Decrease Pain, Fever, and Inflammation PrepU

View Set

Chapter 4: Final Exam Review Questions

View Set