init
This commit is contained in:
69
lab/lab2/Question1/SumAndAverage.java
Normal file
69
lab/lab2/Question1/SumAndAverage.java
Normal file
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022-03-02
|
||||
* Assignment: #1 Question 1
|
||||
* Description: This program is used to calculate the sum and average of the numbers.
|
||||
* Input: The numbers
|
||||
* Output: The sum and average of the numbers
|
||||
* Version: 1.0
|
||||
*/
|
||||
public class SumAndAverage {
|
||||
public static void main(String[] args) {
|
||||
int sum = 0; // Store the accumulated sum init to 0
|
||||
double average; // average in double
|
||||
int lowerbound = 1; // The lowerbound to sum
|
||||
int upperbound = 100; // The upperbound to sum
|
||||
|
||||
// Use a for-loop to sum from lowerbound to upperbound
|
||||
for (int number = lowerbound; number <= upperbound; number++) {
|
||||
// add only odds number
|
||||
if (number % 2 != 0) {
|
||||
sum += number;
|
||||
}
|
||||
}
|
||||
|
||||
// Compute average in double. Beware that int/int produces int.
|
||||
average = (double) sum / (upperbound - lowerbound + 1);
|
||||
|
||||
// Print sum and average
|
||||
System.out.println("The sum is " + sum);
|
||||
System.out.println("The average is " + average);
|
||||
|
||||
// 6.1 while loop
|
||||
System.out.println("---- While-loop:");
|
||||
sum = 0;
|
||||
int number = lowerbound;
|
||||
while (number <= upperbound) {
|
||||
// only add number that is divisible by 7
|
||||
if (number % 7 == 0) {
|
||||
sum += number;
|
||||
}
|
||||
number++;
|
||||
}
|
||||
|
||||
// Compute average in double. Beware that int/int produces int.
|
||||
average = (double) sum / (upperbound - lowerbound + 1);
|
||||
|
||||
// Print sum and average
|
||||
System.out.println("The sum is " + sum);
|
||||
System.out.println("The average is " + average);
|
||||
|
||||
// 6.2 do-while
|
||||
System.out.println("---- Do-while:");
|
||||
sum = 0; // Reset sum
|
||||
number = lowerbound;
|
||||
do {
|
||||
// sum of squares of number
|
||||
sum += number * number;
|
||||
number++;
|
||||
} while (number <= upperbound);
|
||||
|
||||
// Compute average in double. Beware that int/int produces int.
|
||||
average = (double) sum / (upperbound - lowerbound + 1);
|
||||
|
||||
// Print sum and average
|
||||
System.out.println("The sum is " + sum);
|
||||
System.out.println("The average is " + average);
|
||||
|
||||
}
|
||||
}
|
||||
65
lab/lab2/Question2/TimeTable.java
Normal file
65
lab/lab2/Question2/TimeTable.java
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Authro: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022-03-02
|
||||
* Description: A table of multiplication table of 1 to 9.
|
||||
* Using nested for loop to print the table.
|
||||
* Assignemnt: #2 Question2
|
||||
* Output:
|
||||
|
||||
* | 1 2 3 4 5 6 7 8 9
|
||||
---------------------------------------
|
||||
1 | 1 2 3 4 5 6 7 8 9
|
||||
2 | 2 4 6 8 10 12 14 16 18
|
||||
3 | 3 6 9 12 15 18 21 24 27
|
||||
4 | 4 8 12 16 20 24 28 32 36
|
||||
5 | 5 10 15 20 25 30 35 40 45
|
||||
6 | 6 12 18 24 30 36 42 48 54
|
||||
7 | 7 14 21 28 35 42 49 56 63
|
||||
8 | 8 16 24 32 40 48 56 64 72
|
||||
9 | 9 18 27 36 45 54 63 72 81
|
||||
|
||||
*/
|
||||
|
||||
public class TimeTable {
|
||||
public static void printHeader() {
|
||||
System.out.println("* | 1 2 3 4 5 6 7 8 9");
|
||||
System.out.println("---------------------------------------");
|
||||
}
|
||||
public static void printLineBegin(int i) {
|
||||
System.out.print(i + " |");
|
||||
}
|
||||
public static int spaceBefore(int i) {
|
||||
// format the number to be printed
|
||||
String s = Integer.toString(i);
|
||||
// calculate the number of spaces before the number
|
||||
int len = s.length();
|
||||
// return the number of spaces
|
||||
int space = 4 - len;
|
||||
return space;
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
// print the two lines header
|
||||
printHeader();
|
||||
|
||||
// loop for main content
|
||||
for (int i = 1; i <= 9; i++) {
|
||||
|
||||
printLineBegin(i);
|
||||
|
||||
// loop for each number in the line
|
||||
for (int j = 1; j <= 9; j++) {
|
||||
int number = i * j;
|
||||
|
||||
// print the space before the number
|
||||
for (int k = 0; k < spaceBefore(number); k++) {
|
||||
System.out.print(" ");
|
||||
}
|
||||
|
||||
// print the number
|
||||
System.out.print(number);
|
||||
}
|
||||
// print the end of the line
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
43
lab/lab2/Question3/TestPalindrome.java
Normal file
43
lab/lab2/Question3/TestPalindrome.java
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022-03-02
|
||||
* Description: Test whether a sentence is a palindrome, iguore the case punctuation and spaces.
|
||||
* Assignment: #2 Question 3
|
||||
*/
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class TestPalindrome {
|
||||
public static void main(String[] args) {
|
||||
System.out.print("Enter a String: ");
|
||||
Scanner in = new Scanner(System.in);
|
||||
|
||||
// read a line of input from the user
|
||||
String inStr = in.nextLine();
|
||||
|
||||
// close the scanner object
|
||||
in.close();
|
||||
|
||||
// convert the string to lower case
|
||||
String str = inStr.toLowerCase();
|
||||
|
||||
// remove all the punctuation using Character.isLetter()
|
||||
// str = str.replaceAll("[^a-zA-Z0-9]", "");
|
||||
String str2 = str;
|
||||
str = "";
|
||||
for (int i = 0; i < str2.length(); i++) {
|
||||
if (Character.isLetter(str2.charAt(i))) {
|
||||
str += str2.charAt(i);
|
||||
}
|
||||
}
|
||||
|
||||
// reverse the string
|
||||
String reverse = new StringBuffer(str).reverse().toString();
|
||||
|
||||
// compare the two strings
|
||||
// if they are the same, the string is a palindrome
|
||||
// otherwise, it is not
|
||||
System.out.format("\"%s\" is %s a palindrome.\n", inStr,
|
||||
str.equals(reverse) ? "" : "not ");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user