(17170908) JAVA - Practice 1
(17170908) JAVA - Practice 1
ID: 17170908
Problem 1:
A program that prompts the user to enter a letter and displays its corresponding number.
Source code:
package practice1;
import java.util.Scanner;
String s = input.nextLine();
char ch = s.charAt(0);
ch = Character.toUpperCase(ch);
int number = 0;
if (Character.isLetter(ch)) {
if (ch >= 'W') {
number = 9;
number = 8;
number = 7;
number = 6;
number = 5;
number = 4;
number = 3;
number = 2;
} else {
Input:
1. Enter a letter: X
2. Enter a letter: h
Output:
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.
Source code:
package practice1;
import java.util.Scanner;
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");
}
}
}
Input:
Output:
1. Mathematics Sophomore
2. Computer Science Senior
Run Screenshot:
Problem 3:
A program that prompts the user to enter three cities and displays them in ascending order.
Source code:
package practice1;
import java.util.Scanner;
String temp;
temp = city1;
city1 = city2;
city2 = temp;
temp = city1;
city1 = city3;
city3 = temp;
}
if (city3.compareTo(city2) < 0) {
temp = city2;
city2 = city3;
city3 = temp;
Input:
Output:
A program that reads an unspecified number of integers, determines how many positive and negative
values have been read, and computes the total and average of the input values (not counting zeros). Your
program ends with the input 0. Display the average as a floating-point number.
Source code:
package practice1;
import java.util.Scanner;
number = input.nextInt();
}
Input:
Output:
Problem 5:
Homework 2.3, SubtractionQuizLoop.java generates five random subtraction questions. Revise the
program to generate ten random addition questions for two integers between 1 and 15. Display the correct
count and test time.
Source code:
package practice1;
import java.util.Scanner;
System.out.print(
} else {
count++;
Input:
What is 3 + 14? 17
What is 3 + 12? 19
What is 8 + 12? 20
What is 14 + 7? 21
What is 15 + 14? 30
What is 15 + 14? 29
What is 3 + 15? 16
What is 10 + 5? 76
What is 13 + 5? 23
What is 4 + 13? 17
Output:
What is 3 + 14? 17
You are correct!
What is 3 + 12? 19
Your answer is wrong.
3 + 12 should be 15
What is 8 + 12? 20
You are correct!
What is 14 + 7? 21
You are correct!
What is 15 + 14? 30
Your answer is wrong.
15 + 14 should be 29
What is 15 + 14? 29
You are correct!
What is 3 + 15?
16
Your answer is wrong.
3 + 15 should be 18
What is 10 + 5? 76
Your answer is wrong.
10 + 5 should be 15
What is 13 + 5? 23
Your answer is wrong.
13 + 5 should be 18
What is 4 + 13? 17
You are correct!
Correct count is 5
Test time is 403 seconds
3+14=17 correct
3+12=19 wrong
8+12=20 correct
14+7=21 correct
15+14=30 wrong
15+14=29 correct
3+15=16 wrong
10+5=76 wrong
13+5=23 wrong
4+13=17 correct
Run Screenshot:
Problem 6:
Source code:
package practice1;
// Init
int row = 0;
int maxRows = 8;
int num = 1;
// Printing loop
// Indent
// Print nums
// New line
System.out.println("");
// Adjustments
++row;
--indent;
++num;
Output:
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
1 2 4 8 16 32 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
Run Screenshot:
Problem 7:
To display all the prime numbers between 2 and 1,000, inclusive. Display eight prime numbers per line.
Numbers are separated by exactly one space.
Source code:
package practice1;
System.out.println(
}
// Display the prime number and increase the count
if (isPrime) {
if (count % NUMBER_OF_PRIMES_PER_LINE == 0) {
System.out.println(number);
} else {
Output:
2 3 5 7 11 13 17 19
23 29 31 37 41 43 47 53
59 61 67 71 73 79 83 89
97 101 103 107 109 113 127 131
137 139 149 151 157 163 167 173
179 181 191 193 197 199 211 223
227 229 233 239 241 251 257 263
269 271 277 281 283 293 307 311
313 317 331 337 347 349 353 359
367 373 379 383 389 397 401 409
419 421 431 433 439 443 449 457
461 463 467 479 487 491 499 503
509 521 523 541 547 557 563 569
571 577 587 593 599 601 607 613
617 619 631 641 643 647 653 659
661 673 677 683 691 701 709 719
727 733 739 743 751 757 761 769
773 787 797 809 811 821 823 827
829 839 853 857 859 863 877 881
883 887 907 911 919 929 937 941
947 953 967 971 977 983 991 997
Run Screenshot:
Problem 8:
A program that prompts the user to enter the year and first day of the year and displays the calendar table
for the year on the console.
Source code:
package practice1;
import java.util.Scanner;
int numberOfDaysInMonth = 0;
for (int month = 1; month <= 12; month++) {
System.out.print(" ");
switch (month) {
case 1:
System.out.println("January " + year);
numberOfDaysInMonth = 31;
break;
case 2:
System.out.println("February " + year);
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
numberOfDaysInMonth = 29;
} else {
numberOfDaysInMonth = 28;
}
break;
case 3:
System.out.println("March " + year);
numberOfDaysInMonth = 31;
break;
case 4:
System.out.println("April " + year);
numberOfDaysInMonth = 30;
break;
case 5:
System.out.println("May " + year);
numberOfDaysInMonth = 31;
break;
case 6:
System.out.println("June " + year);
numberOfDaysInMonth = 30;
break;
case 7:
System.out.println("July " + year);
numberOfDaysInMonth = 31;
break;
case 8:
System.out.println("August " + year);
numberOfDaysInMonth = 31;
break;
case 9:
System.out.println("September " + year);
numberOfDaysInMonth = 30;
break;
case 10:
System.out.println("October " + year);
numberOfDaysInMonth = 31;
break;
case 11:
System.out.println("November " + year);
numberOfDaysInMonth = 30;
break;
case 12:
System.out.println("December " + year);
numberOfDaysInMonth = 31;
}
System.out.println("-----------------------------");
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
}
Input:
Output:
Run Screenshot:
Problem 9:
package practice1;
import java.util.Scanner;
package practice1;
import java.util.Scanner;
// Display results
System.out.println("The octal of " + decimal + " is " + octal);
}
}
Input:
Output:
(Decimal to binary)
(Decimal to octal)
Problem 10:
A program that prompts the user to enter two strings and displays the largest common prefix of the two
strings.
Source code:
package practice1;
import java.util.Scanner;
prefix += string1.charAt(index);
index++;
} else {
Input:
Output:
To do all these program, I faced many problems but I solved that by myself. These kind of exercise will
help us to improve our skills.
Finally, I want to say that, I enjoyed a lot to solve all these problem.