//-----------
Introduction to Java Programming, Tenth Edition, Y. Daniel Liang
import java.util.Scanner;
public class TestLoanClass {
/** Main method */
public static void main(String[] args) {
// Create a Scanner
Scanner input = new Scanner(System.in);
// Enter yearly interest rate
System.out.print(
"Enter yearly interest rate, for example, 8.25: ");
double annualInterestRate = input.nextDouble();
// Enter number of years
System.out.print("Enter number of years as an integer: ");
int numberOfYears = input.nextInt();
// Enter loan amount
System.out.print("Enter loan amount, for example, 120000.95: ");
double loanAmount = input.nextDouble();
// Create Loan object
Loan loan =
new Loan(annualInterestRate, numberOfYears, loanAmount);
// Display loan date, monthly payment, and total payment
System.out.printf("The loan was created on %s\n" +
"The monthly payment is %.2f\nThe total payment is %.2f\n",
loan.getLoanDate().toString(), loan.getMonthlyPayment(),
loan.getTotalPayment());
}
}
//----------
Introduction to Java Programming, Tenth Edition, Y. Daniel Liang
public class Loan {
private double annualInterestRate;
private int numberOfYears;
private double loanAmount;
private java.util.Date loanDate;
/** Default constructor */
public Loan() {
this(2.5, 1, 1000);
}
/** Construct a loan with specified annual interest rate,
number of years, and loan amount
*/
public Loan(double annualInterestRate, int numberOfYears,
double loanAmount) {
this.annualInterestRate = annualInterestRate;
this.numberOfYears = numberOfYears;
this.loanAmount = loanAmount;
loanDate = new java.util.Date();
}
/** Return annualInterestRate */
public double getAnnualInterestRate() {
return annualInterestRate;
}
/** Set a new annualInterestRate */
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
/** Return numberOfYears */
public int getNumberOfYears() {
return numberOfYears;
}
/** Set a new numberOfYears */
public void setNumberOfYears(int numberOfYears) {
this.numberOfYears = numberOfYears;
}
/** Return loanAmount */
public double getLoanAmount() {
return loanAmount;
}
/** Set a newloanAmount */
public void setLoanAmount(double loanAmount) {
this.loanAmount = loanAmount;
}
/** Find monthly payment */
public double getMonthlyPayment() {
double monthlyInterestRate = annualInterestRate / 1200;
double monthlyPayment = loanAmount * monthlyInterestRate / (1 -
(1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12)));
return monthlyPayment;
}
/** Find total payment */
public double getTotalPayment() {
double totalPayment = getMonthlyPayment() * numberOfYears * 12;
return totalPayment;
}
/** Return loan date */
public java.util.Date getLoanDate() {
return loanDate;
}
}
//-----------
Introduction to Java Programming, Tenth Edition, Y. Daniel Liang
public class UseBMIClass {
public static void main(String[] args) {
BMI bmi1 = new BMI("John Doe", 18, 145, 70);
System.out.println("The BMI for " + bmi1.getName() + " is "
+ bmi1.getBMI() + " " + bmi1.getStatus());
BMI bmi2 = new BMI("Peter King", 215, 70);
System.out.println("The BMI for " + bmi2.getName() + " is "
+ bmi2.getBMI() + " " + bmi2.getStatus());
}
}
//-----------
Introduction to Java Programming, Tenth Edition, Y. Daniel Liang
public class BMI {
private String name;
private int age;
private double weight; // in pounds
private double height; // in inches
public static final double KILOGRAMS_PER_POUND = 0.45359237;
public static final double METERS_PER_INCH = 0.0254;
public BMI(String name, int age, double weight, double height) {
this.name = name;
this.age = age;
this.weight = weight;
this.height = height;
}
public BMI(String name, double weight, double height) {
this(name, 20, weight, height);
}
public double getBMI() {
double bmi = weight * KILOGRAMS_PER_POUND /
((height * METERS_PER_INCH) * (height * METERS_PER_INCH));
return Math.round(bmi * 100) / 100.0;
}
public String getStatus() {
double bmi = getBMI();
if (bmi < 18.5)
return "Underweight";
else if (bmi < 25)
return "Normal";
else if (bmi < 30)
return "Overweight";
else
return "Obese";
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public double getWeight() {
return weight;
}
public double getHeight() {
return height;
}
}
//-------------
Introduction to Java Programming, Tenth Edition, Y. Daniel Liang
public class TestCourse {
public static void main(String[] args) {
Course course1 = new Course("Data Structures");
Course course2 = new Course("Database Systems");
course1.addStudent("Peter Jones");
course1.addStudent("Brian Smith");
course1.addStudent("Anne Kennedy");
course2.addStudent("Peter Jones");
course2.addStudent("Steve Smith");
System.out.println("Number of students in course1: "
+ course1.getNumberOfStudents());
String[] students = course1.getStudents();
for (int i = 0; i < course1.getNumberOfStudents(); i++)
System.out.print(students[i] + ", ");
System.out.println();
System.out.print("Number of students in course2: "
+ course2.getNumberOfStudents());
}
}
//-------------
Introduction to Java Programming, Tenth Edition, Y. Daniel Liang
public class Course {
private String courseName;
private String[] students = new String[100];
private int numberOfStudents;
public Course(String courseName) {
this.courseName = courseName;
}
public void addStudent(String student) {
students[numberOfStudents] = student;
numberOfStudents++;
}
public String[] getStudents() {
return students;
}
public int getNumberOfStudents() {
return numberOfStudents;
}
public String getCourseName() {
return courseName;
}
public void dropStudent(String student) {
// Left as an exercise in Exercise 10.9
}
}
//--------------
Introduction to Java Programming, Tenth Edition, Y. Daniel Liang
public class TestStackOfIntegers {
public static void main(String[] args) {
StackOfIntegers stack = new StackOfIntegers();
for (int i = 0; i < 10; i++)
stack.push(i);
while (!stack.empty())
System.out.print(stack.pop() + " ");
}
}
//------------
Introduction to Java Programming, Tenth Edition, Y. Daniel Liang
public class StackOfIntegers {
private int[] elements;
private int size;
public static final int DEFAULT_CAPACITY = 16;
/** Construct a stack with the default capacity 16 */
public StackOfIntegers() {
this(DEFAULT_CAPACITY);
}
/** Construct a stack with the specified maximum capacity */
public StackOfIntegers(int capacity) {
elements = new int[capacity];
}
/** Push a new integer into the top of the stack */
public void push(int value) {
if (size >= elements.length) {
int[] temp = new int[elements.length * 2];
System.arraycopy(elements, 0, temp, 0, elements.length);
elements = temp;
}
elements[size++] = value;
}
/** Return and remove the top element from the stack */
public int pop() {
return elements[--size];
}
/** Return the top element from the stack */
public int peek() {
return elements[size - 1];
}
/** Test whether the stack is empty */
public boolean empty() {
return size == 0;
}
/** Return the number of elements in the stack */
public int getSize() {
return size;
}
}
//-------------
Introduction to Java Programming, Tenth Edition, Y. Daniel Liang
import java.math.*;
public class LargeFactorial {
public static void main(String[] args) {
System.out.println("50! is \n" + factorial(50));
}
public static BigInteger factorial(long n) {
BigInteger result = BigInteger.ONE;
for (int i = 1; i <= n; i++)
result = result.multiply(new BigInteger(i+""));
return result;
}
}
//--------------
Introduction to Java Programming, Tenth Edition, Y. Daniel Liang
import java.util.Scanner;
public class PalindromeIgnoreNonAlphanumeric {
/** Main method */
public static void main(String[] args) {
// Create a Scanner
Scanner input = new Scanner(System.in);
// Prompt the user to enter a string
System.out.print("Enter a string: ");
String s = input.nextLine();
// Display result
System.out.println("Ignoring non-alphanumeric characters, \nis "
+ s + " a palindrome? " + isPalindrome(s));
}
/** Return true if a string is a palindrome */
public static boolean isPalindrome(String s) {
// Create a new string by eliminating non-alphanumeric chars
String s1 = filter(s);
// Create a new string that is the reversal of s1
String s2 = reverse(s1);
// Compare if the reversal is the same as the original string
return s2.equals(s1);
}
/** Create a new string by eliminating non-alphanumeric chars */
public static String filter(String s) {
// Create a string builder
StringBuilder stringBuilder = new StringBuilder();
// Examine each char in the string to skip alphanumeric char
for (int i = 0; i < s.length(); i++) {
if (Character.isLetterOrDigit(s.charAt(i))) {
stringBuilder.append(s.charAt(i));
}
}
// Return a new filtered string
return stringBuilder.toString();
}
/** Create a new string by reversing a specified string */
public static String reverse(String s) {
StringBuilder stringBuilder = new StringBuilder(s);
stringBuilder.reverse(); // Invoke reverse in StringBuilder
return stringBuilder.toString();
}
}
//---------
No comments:
Post a Comment