Check if valid date and convert phone number from letters to numbers

Assignment

Solution

Assignment1
Question1.java

package assignment1;

importjava.util.Scanner;

public class Question1 {

Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {
Question1 question1 = new Question1();
question1.run();
}

private void run() {

String date = scanner.nextLine();
String[] dateItems = date.split(“/”);

int month = Integer.parseInt(dateItems[0]);
int day = Integer.parseInt(dateItems[1]);
int year = Integer.parseInt(dateItems[2]);

String output = date;
if (!monthIsValid(month) || !dayIsValid(day, month, year)) {
output += ” is not valid:\n”;
if (!monthIsValid(month)) {
output += “month value ” + month + ” is incorrect;\n”;
} else if (!dayIsValid(day, month, year)) {
output += “day value ” + day + ” is incorrect for month = ” + month + ” and year = ” + year;
}
} else {
output += ” is valid”;
}

System.out.println(output);
}

privatebooleanmonthIsValid(int month) {
return month > 0 && month < 13; } privatebooleandayIsValid(int day, int month, int year) { switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: { return day > 0 && day < 32; } case 4: case 6: case 9: case 11: { return day > 0 && day < 31; } case 2: { if (year % 4 == 0 && year % 100 != 0) { return day > 0 && day < 30; } else { return day > 0 && day < 29; } } default: { return false; } } } } Question2.java package assignment1; importjava.util.Scanner; public class Question2 { privateint max = Integer.MIN_VALUE; privateint min = Integer.MAX_VALUE; privateint sum = 0; privateint count = 0; private Scanner scanner = new Scanner(System.in); public static void main(String[] args) { Question2 question2 = new Question2(); question2.run(); } private void run() { System.out.println(“Please enter non-negative integers.\nNegative integer means the end of the input.”); intcurrentInt = 0; while (currentInt>= 0) {
currentInt = Integer.parseInt(scanner.nextLine());

if (currentInt< 0 ) { continue; } if (currentInt> max) {
max = currentInt;
}

if (currentInt< min) {
min = currentInt;
}

sum += currentInt;
count++;
}

double average = (double) sum / count;

System.out.println(“Max: ” + max);
System.out.println(“Min: ” + min);
System.out.println(“Average: ” + String.format(“%.2f”, average));
}
}

Question3.java

package assignment1;

importjava.util.Scanner;

public class Question3 {

private Scanner scanner = new Scanner(System.in);
char[] symbolsToProcess = new char[7];
char[] processedSymbols = new char[7];

public static void main(String[] args) {
Question3 question3 = new Question3();
question3.run();
}

private void run() {

initSymbolsToProcess();
setProcessedSymbols();

String output = “”;
for (int i = 0; i < 7; i++) {
output += processedSymbols[i];
if (i == 2) {
output += ‘-‘;
}
}

System.out.println(output);
}

private void initSymbolsToProcess() {

String input = scanner.nextLine();
String[] words = input.split(” “);
for (int i = 0; i <words.length; i++) {
words[i] = words[i].trim();
}

String processedInput = “”;
for (int i = 0; i <words.length; i++) {
processedInput += words[i];
}

for (int i = 0; i < 7; i++) {
symbolsToProcess[i] = processedInput.charAt(i);
}
}

private void setProcessedSymbols() {
for (int i = 0; i < 7; i++) {
processedSymbols[i] = processSymbol(symbolsToProcess[i]);
}
}

private char processSymbol(char ch) {
switch (ch) {
case ‘a’:
case ‘A’:
case ‘b’:
case ‘B’:
case ‘c’:
case ‘C’: {
return ‘2’;
}
case ‘d’:
case ‘D’:
case ‘e’:
case ‘E’:
case ‘f’:
case ‘F’: {
return ‘3’;
}
case ‘g’:
case ‘G’:
case ‘h’:
case ‘H’:
case ‘i’:
case ‘I’: {
return ‘4’;
}
case ‘j’:
case ‘J’:
case ‘k’:
case ‘K’:
case ‘l’:
case ‘L’: {
return ‘5’;
}
case ‘m’:
case ‘M’:
case ‘n’:
case ‘N’:
case ‘o’:
case ‘O’: {
return ‘6’;
}
case ‘p’:
case ‘P’:
case ‘q’:
case ‘Q’:
case ‘r’:
case ‘R’:
case ‘s’:
case ‘S’: {
return ‘7’;
}
case ‘t’:
case ‘T’:
case ‘u’:
case ‘U’:
case ‘v’:
case ‘V’: {
return ‘8’;
}
case ‘w’:
case ‘W’:
case ‘x’:
case ‘X’:
case ‘y’:
case ‘Y’:
case ‘z’:
case ‘Z’: {
return ‘9’;
}
default: {
return ‘?’;
}
}
}
}

Assignment2

Question1.java

package assignment2;

importjava.util.Scanner;

public class Question1 {

private Scanner scanner = new Scanner(System.in);
char[] symbolsToProcess;
char[] processedSymbols;
private String input = “input”;

public static void main(String[] args) {
Question1 question1 = new Question1();
question1.run();

}

private void run() {

while (!””.equals(input) && input != null) {

System.out.println(“Please enter the string to process, enter the empty string to exit the program:”);

initSymbolsToProcess();

if (“”.equals(input) || input == null) {
continue;
}

setProcessedSymbols();

String output = “”;
for (int i = 0; i <processedSymbols.length; i++) {
output += processedSymbols[i];
if (i == 2 || (i – 2) % 4 == 0) {
output += ‘-‘;
}
}

System.out.println(output);
}
}

private void initSymbolsToProcess() {

input = scanner.nextLine();

if (“”.equals(input) || input == null) {
return;
}

String[] words = input.split(” “);
for (int i = 0; i <words.length; i++) {
words[i] = words[i].trim();
}

String processedInput = “”;
for (int i = 0; i <words.length; i++) {
processedInput += words[i];
}

symbolsToProcess = new char[processedInput.length()];

for (int i = 0; i <symbolsToProcess.length; i++) {
symbolsToProcess[i] = processedInput.charAt(i);
}
}

private void setProcessedSymbols() {

processedSymbols = new char[symbolsToProcess.length];

for (int i = 0; i <symbolsToProcess.length; i++) {
processedSymbols[i] = processSymbol(symbolsToProcess[i]);
}
}

private char processSymbol(char ch) {
switch (ch) {
case ‘a’:
case ‘A’:
case ‘b’:
case ‘B’:
case ‘c’:
case ‘C’: {
return ‘2’;
}
case ‘d’:
case ‘D’:
case ‘e’:
case ‘E’:
case ‘f’:
case ‘F’: {
return ‘3’;
}
case ‘g’:
case ‘G’:
case ‘h’:
case ‘H’:
case ‘i’:
case ‘I’: {
return ‘4’;
}
case ‘j’:
case ‘J’:
case ‘k’:
case ‘K’:
case ‘l’:
case ‘L’: {
return ‘5’;
}
case ‘m’:
case ‘M’:
case ‘n’:
case ‘N’:
case ‘o’:
case ‘O’: {
return ‘6’;
}
case ‘p’:
case ‘P’:
case ‘q’:
case ‘Q’:
case ‘r’:
case ‘R’:
case ‘s’:
case ‘S’: {
return ‘7’;
}
case ‘t’:
case ‘T’:
case ‘u’:
case ‘U’:
case ‘v’:
case ‘V’: {
return ‘8’;
}
case ‘w’:
case ‘W’:
case ‘x’:
case ‘X’:
case ‘y’:
case ‘Y’:
case ‘z’:
case ‘Z’: {
return ‘9’;
}
default: {
return ‘?’;
}
}
}
}

Question2.java

package assignment2;

public class Question2 {

private String[] names;
privateint[][] scores;
private char[] grades;

public static void main(String[] args) {
Question2 question2 = new Question2();
question2.run();
}

private void run() {

names = initNames();
scores = initScores();
grades = initGrades();
printResults();

}

private char calculateGrade(int[] arr) {

doubleavg = calculateAverageTestScore(arr);
char grade;

if (avg< 50) {
grade = ‘F’;
} else if (avg< 65) {
grade = ‘D’;
} else if (avg< 75) {
grade = ‘C’;
} else if (avg< 85) {
grade = ‘B’;
} else {
grade = ‘A’;
}

return grade;
}

private double calculateAverageTestScore(int[] arr) {

int tot = 0;
for (int i = 0; i <arr.length; i++) {
tot += arr[i];
}

return (double) tot / arr.length;
}

private String[] initNames() {
String[] arr = {“Johnson”, “Aniston”, “Cooper”, “Gupta”, “Blair”,
“Clark”, “Kennedy”, “Bronson”, “Sunny”, “Smith”};
returnarr;
}

privateint[][] initScores() {
int[][] arr = {
{85, 83, 77, 91, 76},
{80, 90, 95, 93, 48},
{78, 81, 11, 90, 73},
{92, 83, 30, 69, 87},
{23, 45, 96, 38, 59},
{60, 85, 45, 39, 67},
{77, 31, 52, 74, 83},
{93, 94, 89, 77, 97},
{79, 85, 28, 93, 82},
{85, 72, 49, 75, 63}
};
returnarr;
}

private char[] initGrades() {
char[] grades = new char[names.length];
for (int i = 0; i <grades.length; i++) {
grades[i] = calculateGrade(scores[i]);
}

return grades;
}

private double calculateClassAverage() {

int tot = 0;

for (int i = 0; i <scores.length; i++) {
for (int j = 0; j < scores[0].length; j++) {
tot += scores[i][j];
}
}

return (double) tot / (scores.length * scores[0].length);
}

private void printResults() {

for (int i = 0; i <scores.length; i++) {

System.out.print(names[i]);
for (int j = 0; j < 10 – names[i].length(); j++) {
System.out.print(” “);
}

for (int j = 0; j < scores[0].length; j++) {
System.out.print(” ” + scores[i][j]);
}

System.out.print(” ” + calculateAverageTestScore(scores[i]));
System.out.print(” ” + grades[i]);

System.out.println();
}
}
}