//Introduction to Java Programming
//p30/603 2021年03月21日 星期日 13时46分12秒
import javax.swing.JOptionPane;
import java.util.Scanner;
public class hello{
/* public static void main(String[] args){
System.out.println(“Welcome to Java!”);
}
wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
Welcome to Java!
/
public static void main(String[] args){
//Welcome1.java 2021年03月21日 星期日 13时49分25秒
/ System.out.println(“Programming is fun!”);
System.out.println(“Fundamentals First!”);
System.out.println(“Problem Driven!”);
wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
Programming is fun!
Fundamentals First!
Problem Driven!
//ComputeExpression.java
System.out.println((10.5 + 2* 3)/(45- 3.5));
wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
0.39759036144578314
//WelcomInMessageDialogBox.java
// JOptionPane.showMessageDialog(null, "welcome to Java!");
//ComputerArea.java 2021年03月21日 星期日 14时03分47秒
double radius;
double area;
radius = 20;
area = radius * radius * 3.14159;
System.out.println("The area for the circle of radius " + radius + " is "+area);
wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
The area for the circle of radius 20.0 is 1256.636
Scanner input = new Scanner(System.in);
System.out.print("Enter a number for radius: ");
double radius = input.nextDouble();
double area = radius * radius * 3.14159;
System.out.println("The area for the circle of radius "+ radius +" is " + area);
wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
Enter a number for radius: 15.5
The area for the circle of radius 15.5 is 754.7669975
//ComputerAverage.java
Scanner input = new Scanner(System.in);
System.out.print("Enter three numbers: ");
double number1 = input.nextDouble();
double number2 = input.nextDouble();
double number3 = input.nextDouble();
double average = (number1 + number2 + number3)/3;
System.out.println("The average of " + number1 + " " + number2 +
" " + number3 + " is " + average);
wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
Enter three numbers: 15.5
16.5
17.5
The average of 15.5 16.5 17.5 is 16.5
//ComputeArea.java: Compute the area of a circle
double radius = 20;
final double PI = 3.14159;
double area = radius * radius * PI;
System.out.println("The area for the circle of radius " + radius + " is "+area);
wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
The area for the circle of radius 20.0 is 1256.636
//DisplayTime.java 2021年03月21日 星期日 14时16分59秒
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer for seconds: ");
int seconds = input.nextInt();
int minutes = seconds / 60;
int remainingSeconds = seconds % 60;
System.out.println(seconds + " seconds is " + minutes + " minutes and " +
remainingSeconds + " seconds");
wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
Enter an integer for seconds: 12125
12125 seconds is 202 minutes and 5 seconds
//FahrenheitToCelsius.java 2021年03月21日 星期日 14时18分20秒
Scanner input = new Scanner(System.in);
System.out.print("Enter a degree in Fahrenheit: ");
double fahrenheit = input.nextDouble();
double celsius = (5.0 / 9)* (fahrenheit - 32);
System.out.println("Fahrenheit "+ fahrenheit + " is " +
celsius + " in Celsius");
wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
Enter a degree in Fahrenheit: 100
Fahrenheit 100.0 is 37.77777777777778 in Celsius
//ShowCurrentTime.java 2021年03月21日 星期日 14时33分29秒
long totalMilliseconds = System.currentTimeMillis();
long totalSeconds = totalMilliseconds / 1000;
long currentSecond = totalSeconds % 60;
long totalMinutes = totalSeconds / 60;
long currentMinute = totalMinutes % 60;
long totalHours = totalMinutes / 60;
long currentHour = totalHours % 24;
System.out.println("Current time is “+ currentHour + “:”
+ currentMinute + “:” + currentSecond + " GMT”);
wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
Current time is 6:32:51 GMT
//SalesTax.java
Scanner input = new Scanner(System.in);
System.out.print("Enter puchase amount: ");
double purchaseAmount = input.nextDouble();
double tax = purchaseAmount * 0.06;
System.out.println("Sales tax is "+ (int)(tax * 100)/ 100.0);
wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
Enter puchase amount: 197.55
Sales tax is 11.85
//ComputeLoan.java 2021年03月21日 星期日 14时37分59秒
Scanner input = new Scanner(System.in);
System.out.print("Enter yearly interest rate, for example 8.25: ");
double annualInterestRate = input.nextDouble();
double monthlyInterestRate = annualInterestRate / 1200;
System.out.print("Enter number of years as an integer, for example 5: ");
int numberOfYears = input.nextInt();
System.out.print("Enter loan amount, for example 120000.95: ");
double loanAmount = input.nextDouble();
double monthlyPayment = loanAmount * monthlyInterestRate / (1
-1/Math.pow(1 + monthlyInterestRate, numberOfYears* 12));
double totalPayment = monthlyPayment* numberOfYears* 12;
System.out.println("The monthly payment is " +
(int)(monthlyPayment * 100)/ 100.0);
System.out.println("The total payment is " +
(int)(totalPayment * 100)/ 100.0);
wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
Enter yearly interest rate, for example 8.25: 5.75
Enter number of years as an integer, for example 5: 15
Enter loan amount, for example 120000.95: 250000
The monthly payment is 2076.02
The total payment is 373684.53
//DispalyUnicode.java 2021年03月21日 星期日 21时40分12秒
JOptionPane.showMessageDialog(null,
“\u6B22\u8FCE\u03b1\u03b2\u03b3”,
“\u6B22\u8FCE Welcome”,
JOptionPane.INFORMATION_MESSAGE);
//ComputeChange.java
Scanner input = new Scanner(System.in);
System.out.print(
"Enter an amount in double, for example 11.56: ");
double amount = input.nextDouble();
int remainingAmount = (int)(amount * 100);
int numberOfOneDollars = remainingAmount / 100;
remainingAmount = remainingAmount % 100;
int numberOfQuarters = remainingAmount / 25;
remainingAmount = remainingAmount % 25;
int numberOfDimes = remainingAmount / 10;
remainingAmount = remainingAmount % 10;
int numberOfNickels = remainingAmount % 5;
remainingAmount = remainingAmount % 5;
int numberOfPennies = remainingAmount;
System.out.println("Your amount "+ amount + "consists of\n" +
"\t" + numberOfOneDollars + " dollars\n" +
"\t" + numberOfQuarters + " quarters\n" +
"\t" + numberOfDimes + " dimes\n" +
"\t" + numberOfNickels + " nickels\n" +
"\t" + numberOfPennies + " pennies");
wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
Enter an amount in double, for example 11.56: 11.56
Your amount 11.56consists of
11 dollars
2 quarters
0 dimes
1 nickels
1 pennies
//ComputeLoanUsingInputDialog.java 2021年03月21日 星期日 21时55分36秒
String annualInterestRateString = JOptionPane.showInputDialog(
"Enter yearly interest rate, for example 8.25:");
double annualInterestRate = Double.parseDouble(annualInterestRateString);
double monthlyInterestRate = annualInterestRate / 1200;
String numberOfYearsString = JOptionPane.showInputDialog(
"Enter number of years as an integer, \nfor example 5:");
int numberOfYears = Integer.parseInt(numberOfYearsString);
String loanString = JOptionPane.showInputDialog(
"Enter loan amount, for example 120000.95:");
double loanAmount = Double.parseDouble(loanString);
double monthlyPayment = loanAmount * monthlyInterestRate / (1
-1/Math.pow(1 + monthlyInterestRate, numberOfYears * 12));
double totalPayment = monthlyPayment * numberOfYears * 12;
monthlyPayment = (int)(monthlyPayment * 100) / 100.0;
totalPayment = (int)(totalPayment * 100)/ 100.0;
String output = "The monthly payment is " + monthlyPayment +
"\nThe toal payment is " + totalPayment;
JOptionPane.showMessageDialog(null, output);
//AdditionQuiz.java 2021年03月25日 星期四 19时55分38秒
int number1 = (int)(System.currentTimeMillis() % 10);
int number2 = (int)(System.currentTimeMillis() * 7 % 10);
Scanner input = new Scanner(System.in);
System.out.print(
"What is " + number1 + " + " + number2 + " ? ");
int answer = input.nextInt();
System.out.println(
number1 + " + " + number2 + " = " + answer + " is " +
(number1 + number2 == answer));
wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
What is 4 + 8 ? 4
4 + 8 = 4 is false
//SimpleIfDemo.java 2021年03月25日 星期四 19时56分14秒
Scanner input = new Scanner(System.in);
System.out.println("Enter an integer: ");
int number = input.nextInt();
if(number % 5 == 0)
System.out.println("HiFive");
if(number % 2 == 0)
System.out.println("HiEven");
wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
Enter an integer:
30
HiFive
HiEven
//GuessBirthday.java 2021年03月25日 星期四 20时00分43秒
String set1 =
"1 3 5 7\n" +
"9 11 13 15\n" +
"17 19 21 23\n" +
"25 27 29 31";
String set2 =
"2 3 6 7\n" +
"10 11 14 15\n" +
"20 21 22 23\n" +
"28 29 30 31";
String set3 =
"4 5 6 7\n" +
"12 13 14 15\n" +
"20 21 22 23\n" +
"28 29 30 31";
String set4 =
"8 9 10 11\n" +
"12 13 14 15\n" +
"24 25 26 27\n" +
"28 29 30 31";
String set5 =
"16 17 18 19\n" +
"20 21 22 23\n" +
"24 25 26 27\n" +
"28 29 30 31";
int day = 0;
Scanner input = new Scanner(System.in);
System.out.print("Is your birthday in Set1?\n");
System.out.print(set1);
System.out.print("\nEnter 0 for No and 1 for Yes: ");
int answer = input.nextInt();
if(answer == 1)
day += 1;
System.out.print("Is your birthday in Set2?\n");
System.out.print(set2);
System.out.print("\nEnter 0 for No and 1 for Yes: ");
answer = input.nextInt();
if(answer == 1)
day += 2;
System.out.print("Is your birthday in Set3?\n");
System.out.print(set3);
System.out.print("\nEnter 0 for No and 1 for Yes: ");
answer = input.nextInt();
if(answer == 1)
day += 4;
System.out.print("\nIs your birthday in Set4?\n");
System.out.print(set4);
System.out.print("\nEnter 0 for No and 1 for Yes: ");
answer = input.nextInt();
if(answer == 1)
day += 8;
System.out.print("Is your birthday in Set5?\n");
System.out.print(set5);
System.out.print("\nEnter 0 for No and 1 for Yes: ");
answer = input.nextInt();
if(answer == 1)
day += 16;
System.out.println("\nYour birthday is " + day + "!");
wannian07@wannian07-PC:~/Desktop/Introduction to Java Programming$ java hello
Is your birthday in Set1?
1 3 5 7
9 11 13 15
17 19 21 23
25 27 29 31
Enter 0 for No and 1 for Yes: 1
Is your birthday in Set2?
2 3 6 7
10 11 14 15
20 21 22 23
28 29 30 31
Enter 0 for No and 1 for Yes: 1
Is your birthday in Set3?
4 5 6 7
12 13 14 15
20 21 22 23
28 29 30 31
Enter 0 for No and 1 for Yes: 0
Is your birthday in Set4?
8 9 10 11
12 13 14 15
24 25 26 27
28 29 30 31
Enter 0 for No and 1 for Yes: 0
Is your birthday in Set5?
16 17 18 19
20 21 22 23
24 25 26 27
28 29 30 31
Enter 0 for No and 1 for Yes: 1
Your birthday is 19!
//SubtractionQuiz.java 2021年03月26日 星期五 07时26分29秒
int number1 = (int)(Math.random() * 10);
int number2 = (int)(Math.random() * 10);
if(number1 < number2){
int temp = number1;
number1 = number2;
number2 = temp;
}
System.out.print("What is " + number1 + "-" + number2 +"?");
Scanner input = new Scanner(System.in);
int answer = input.nextInt();
if