|
Spring Semester 2004 Reading Assignment Two |
Your second reading assignment (due Feb 2) is Chapter 3: Numerical Data.
Here are the programs from that chapter:
Page 101:
import javax.swing.*;
import java.text.*;
class Ch3Circle {
public static void main( String[] args ) {
final double PI = 3.14159;
String radiusStr;
double radius, area, circumference;
radiusStr = JOptionPane.showInputDialog(null, "Enter radius:");
radius = Double.parseDouble(radiusStr);
//compute area and circumference
area = PI * radius * radius;
circumference = 2.0 * PI * radius;
JOptionPane.showMessageDialog(null, "Given Radius: " + radius + "\n"
+ "Area: " + area+ "\n"
+ "Circumference: " + circumference);
}
}
Page 108:
import javax.swing.*;
import java.text.*;
class Ch3Circle3 {
public static void main( String[] args ) {
final double PI = 3.14159;
String radiusStr;
double radius, area, circumference;
DecimalFormat df = new DecimalFormat("0.000");
//Get input
radiusStr = JOptionPane.showInputDialog(null, "Enter radius:");
radius = Double.parseDouble(radiusStr);
//Compute area and circumference
area = PI * radius * radius;
circumference = 2.0 * PI * radius;
//Display the results
System.out.println("");
System.out.println("Given Radius: " + radius);
System.out.println("Area: " + df.format(area));
System.out.println("Circumference: " + df.format(circumference));
/*
System.out. println("\nGiven Radius: " + radius + "\n"
+ "Area: " + df.format(area)+ "\n"
+ "Circumference: "
+ df.format(circumference));
*/
}
}
Page 112:
import java.io.*;
import java.text.*;
class Ch3Circle4 {
public static void main( String[] args ) throws IOException {
final double PI = 3.14159;
String radiusStr;
double radius, area, circumference;
BufferedReader bufReader;
DecimalFormat df = new DecimalFormat("0.000");
bufReader = new BufferedReader(
new InputStreamReader( System.in ) );
//Get input
System.out.print("Enter radius: ");
radiusStr = bufReader.readLine();
radius = Double.parseDouble(radiusStr);
//Compute area and circumference
area = PI * radius * radius;
circumference = 2.0 * PI * radius;
//Display the results
System.out.println("");
System.out.println("Given Radius: " + radius);
System.out.println("Area: " + df.format(area));
System.out.println("Circumference: " + df.format(circumference));
}
}
Page 116 (for why this works notice the right angle in the figure!):
import javax.swing.*;
import java.text.*;
class Ch3StatueHeight {
public static void main( String[] args ) {
double height; //height of the statue
double distance; //distance between points A and B
double alpha; //angle measured at point A
double beta; //angle measured at point B
double alphaRad; //angle alpha in radians
double betaRad; //angle beta in radians
String inputStr;
//Get three input values
inputStr = JOptionPane.showInputDialog(null,
"Angle alpha (in degree):");
alpha = Double.parseDouble(inputStr);
inputStr = JOptionPane.showInputDialog(null,
"Angle beta (in degree):");
beta = Double.parseDouble(inputStr);
inputStr = JOptionPane.showInputDialog(null,
"Distance between points A and B (ft):");
distance = Double.parseDouble(inputStr);
//compute the height of the tower
alphaRad = Math.toRadians(alpha);
betaRad = Math.toRadians(beta);
height = ( distance * Math.sin(alphaRad) * Math.sin(betaRad) )
/
Math.sqrt( Math.sin(alphaRad + betaRad) *
Math.sin(alphaRad - betaRad) );
DecimalFormat df = new DecimalFormat("0.000");
JOptionPane.showMessageDialog(null,"Estimating the height of the statue"
+ "\n\n"
+ "Angle at point A (deg): " + df.format(alpha) + "\n"
+ "Angle at point B (deg): " + df.format(beta) + "\n"
+ "Distance between A and B (ft): " + df.format(distance)+ "\n"
+ "Estimated height (ft): " + df.format(height));
}
}
Page 120:
import java.util.*;
class Ch3TestCalendar {
public static void main( String[] args ) {
GregorianCalendar cal = new GregorianCalendar();
System.out.println(cal.getTime());
System.out.println("");
System.out.println("YEAR: " + cal.get(Calendar.YEAR));
System.out.println("MONTH: " + cal.get(Calendar.MONTH));
System.out.println("DATE: " + cal.get(Calendar.DATE));
System.out.println("DAY_OF_YEAR: " + cal.get(Calendar.DAY_OF_YEAR));
System.out.println("DAY_OF_MONTH: " + cal.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY_OF_WEEK: " + cal.get(Calendar.DAY_OF_WEEK));
System.out.println("WEEK_OF_YEAR: " + cal.get(Calendar.WEEK_OF_YEAR));
System.out.println("WEEK_OF_MONTH: " + cal.get(Calendar.WEEK_OF_MONTH));
System.out.println("AM_PM: " + cal.get(Calendar.AM_PM));
System.out.println("HOUR: " + cal.get(Calendar.HOUR));
System.out.println("HOUR_OF_DAY: " + cal.get(Calendar.HOUR_OF_DAY));
System.out.println("MINUTE: " + cal.get(Calendar.MINUTE));
}
}
Page 121-122:
import java.util.*;
import java.text.*;
import javax.swing.*;
class Ch3IndependenceDay {
public static void main( String[] args ) {
GregorianCalendar independenceDay
= new GregorianCalendar(1776, Calendar.JULY, 4);
// = new GregorianCalendar(1776, Calendar.AUGUST, 2);
SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
JOptionPane.showMessageDialog(null, "It was signed on "
+ sdf.format(independenceDay.getTime()));
}
}
import java.util.*;
import java.text.*;
import java.io.*;
class Ch3FindDayOfWeek {
public static void main( String[] args ) throws IOException {
String inputStr;
int year, month, day;
GregorianCalendar cal;
SimpleDateFormat sdf;
BufferedReader bufReader;
bufReader = new BufferedReader(
new InputStreamReader( System.in ) );
System.out.print("Year (yyyy): ");
inputStr = bufReader.readLine();
year = Integer.parseInt(inputStr);
System.out.print("Month (1-12): ");
inputStr = bufReader.readLine();
month = Integer.parseInt(inputStr);
System.out.print("Day (1-31): ");
inputStr = bufReader.readLine();
day = Integer.parseInt(inputStr);
cal = new GregorianCalendar(year, month-1, day);
sdf = new SimpleDateFormat("EEEE");
System.out.println("");
System.out.println("Day of Week: " + sdf.format(cal.getTime()));
}
}
import javax.swing.*;
import java.text.*;
class Ch3LoanCalculator {
public static void main (String[] args) {
final int MONTHS_IN_YEAR = 12;
double loanAmount,
annualInterestRate;
double monthlyPayment,
totalPayment;
double monthlyInterestRate;
int loanPeriod;
int numberOfPayments;
String inputStr;
DecimalFormat df = new DecimalFormat("0.00");
//describe the program
System.out.println("This program computes the monthly and total");
System.out.println("payments for a given loan amount, annual ");
System.out.println("interest rate, and loan period.");
System.out.println("Loan amount in dollars and cents, e.g., 12345.50");
System.out.println("Annual interest rate in percentage, e.g., 12.75");
System.out.println("Loan period in number of years, e.g., 15");
System.out.println("\n"); //skip two lines
//get input values
inputStr = JOptionPane.showInputDialog(null,
"Loan Amount (Dollars+Cents):");
loanAmount = Double.parseDouble(inputStr);
inputStr = JOptionPane.showInputDialog(null,
"Annual Interest Rate (e.g., 9.5):");
annualInterestRate = Double.parseDouble(inputStr);
inputStr = JOptionPane.showInputDialog(null,
"Loan Period - # of years:");
loanPeriod = Integer.parseInt(inputStr);
//compute the monthly and total payments
monthlyInterestRate = annualInterestRate / MONTHS_IN_YEAR / 100;
numberOfPayments = loanPeriod * MONTHS_IN_YEAR;
monthlyPayment = (loanAmount * monthlyInterestRate) /
(1 - Math.pow(1/(1 + monthlyInterestRate),
numberOfPayments ) );
totalPayment = monthlyPayment * numberOfPayments;
//display the result
System.out.println("Loan Amount: $" + loanAmount);
System.out.println("Annual Interest Rate: " + annualInterestRate + "%");
System.out.println("Loan Period (years): " + loanPeriod);
System.out.println("\n"); //skip two lines
System.out.println("Monthly payment is $ "
+ df.format(monthlyPayment));
System.out.println(" TOTAL payment is $ "
+ df.format(totalPayment));
}
}
Problem 19 on page 143 would make a good Lab Assignment Two.