课本例题
chapter1
1.1 Welcom
public class Welcom {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Welcome to java!");
}
}
1.2 WelcomWithThreeMessages
public class WelcomWithThreeMessages {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Programming is fun!");
System.out.println("Fundamentals First");
System.out.println("Problem Driven");
}
}
1.3 Test
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("3.5 * 4 / 2 - 2.5 is ");
System.out.println(3.5 * 4 / 2 - 2.5);
}
}
1.4 ComputeExpression
public class ComputeExpression {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("(10.5 + 2 * 3) / (45 - 3.5) = ");
System.out.println((10.5 + 2 * 3) / (45 - 3.5));
}
}
1.5 ShoeRumtimeError
public class ShoeRumtimeError {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(1 / 1);//1 / 0 is wrong
}
}
1.6 ShowLogicError
public class ShowLogicError {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("Celsius 35 is Fahrenheit degree ");
System.out.println((9 / 5.0) * 35 + 32);
}
}
chapter2
2.1 ComputerArea
public class ComputerArea {
public static void main(String[] args) {
// TODO Auto-generated method stub
double radius;
double area;
radius = 20;
area = radius * radius * 3.14159;
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
2.2 ComputerAreaWithConSoleInput
import java.util.Scanner;//Scanner is in the java.util.package
public class ComputerAreaWithConSoleInput {
public static void main(String[] args) {
// TODO Auto-generated method stub
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);
}
}
2.3 ComputeAverage
import java.util.Scanner;
public class ComputeAverage {
public static void main(String[] args) {
// TODO Auto-generated method stub
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);
}
}
2.4 ComputeAreaWithConstant
import java.util.Scanner;
public class ComputeAreaWithConstant {
public static void main(String[] args) {
// TODO Auto-generated method stub
final double PI = 3.14159;
Scanner input = new Scanner(System.in);
System.out.print("Enter a number for radius ");
double radius = input.nextDouble();
double area = radius * radius * PI;
System.out.println("The area of the cirle of radius " + radius +
" is " + area);
}
}
2.5 DisplyTime
import java.util.Scanner;
public class DisplyTime {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter a 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");
}
}
2.6 FahrenheitToCelsius
import java.util.Scanner;
public class FahrenheitToCelsius {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter a degree in Fahrenhiet: ");
double Fahrenhiet = input.nextDouble();
double celsius = (5.0 / 9) * (Fahrenhiet - 32 );
System.out.println("Fahrenhiet " + Fahrenhiet + " is " + celsius + "in celsius");
}
}
2.7 ShowCurrentTime
public class ShowCurrentTime {
public static void main(String[] args) {
// TODO Auto-generated method stub
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");
}
}
2.8 SzleTax
import java.util.Scanner;
public class SzleTax {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("Enter purchase amount: ");
double purchaseAmount = input.nextDouble();
double tax = purchaseAmount * 0.06;
System.out.println("Sales tax is $" + (int)(tax * 100) / 100.0);
}
}
2.9 ComputeLoan
package hello;
import java.util.Scanner;
public class ComputeLoan {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("Enter annul interest rate, e.g., 7.25: ");
double annulInterestRate = input.nextDouble();
double monthlyInterestRate = annulInterestRate / 1200;
System.out.print("Enter number of years as an interge, e.d., 5: ");
int numberOfYears = input.nextInt();
System.out.print("Enter loan amount, e.g., 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);
}
}
2.10 ComputeChange
package hello;
import java.util.Scanner;
public class ComputeChange {
public static void main(String[] args) {
// TODO Auto-generated method stub
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 = (int)remainingAmount / 25;
remainingAmount = remainingAmount % 25;
int numberOfDimes = (int)remainingAmount / 10;
remainingAmount = remainingAmount % 10;
int numberOfNickels = (int)remainingAmount / 5;
remainingAmount = remainingAmount % 5;
int numberOfPennies = remainingAmount;
System.out.println("Your amount " + amount + " consists of");
System.out.println(" " + numberOfOneDollars + " dollars");
System.out.println(" " + numberOfQuarters + " dollars");
System.out.println(" " + numberOfDimes + " dollars");
System.out.println(" " + numberOfNickels + " dollars");
System.out.println(" " + numberOfPennies + " dollars");
}
}