CS 1063 Intro. to Programming
|
int time = 74747; // or some other value in seconds int hours = ...; // fill in for ... int minutes = ...; // fill in for ... int seconds = ...; // fill in for ... System.out.println(time + " seconds is\n " + hours + " hours\n " + minutes + " minutes\n " + seconds + " seconds\n ");
| Two versions | Several runs |
|---|---|
// Hours, minutes and seconds
public class Time {
public static void main(String[] args) {
int time = 74747; // or some other value
int hours = time/3600;
int minutes = (time - hours*3600)/60;
int seconds = (time - hours*3600 - minutes*60);
System.out.println(time + " seconds is\n " +
hours + " hours,\n " +
minutes + " minutes,\n " +
seconds + " seconds\n");
}
}
//---------------------------------------------------
// Hours, minutes and seconds
public class Time {
public static void main(String[] args) {
int time = 74747; // or some other value
int hours = time/3600;
int minutes = time%3600/60;
int seconds = (time%3600)%60;
System.out.println(time + " seconds is\n " +
hours + " hours,\n " +
minutes + " minutes,\n " +
seconds + " seconds\n");
}
} | 74747 seconds is 20 hours, 45 minutes, 47 seconds 33333 seconds is 9 hours, 15 minutes, 33 seconds 55555 seconds is 15 hours, 25 minutes, 55 seconds |
| Is size between 2 and 10? |
|---|
// Range.java: is size between 2 and 10 (inclusive)?
import java.util.Scanner;
public class Range {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(true) {
System.out.print("Type an int for size ---> ");
int size = in.nextInt();
if (size == 0) break; // 0 terminates loop
// Question 2 above
if ( 2 <= size && size <= 10)
System.out.println(size + " in range");
// Question 3 above (3 separate answers -- study them all!)
if (!(2 <= size && size <= 10))
System.out.println(size + " out of range (version 1)");
if (!(2 <= size) || !(size <= 10))
System.out.println(size + " out of range (version 2)");
if ( 2 > size || size > 10)
System.out.println(size + " out of range (version 3)");
}
}
}
|
| A run (input in bold) |
1 1 out of range (version 1) 1 out of range (version 2) 1 out of range (version 3) 2 2 in range 6 6 in range 10 10 in range 11 11 out of range (version 1) 11 out of range (version 2) 11 out of range (version 3) 0 |
int i = 1;
int sum = 0;
while (i < 10) {
sum = sum + i*i;
i++;
}
System.out.println("sum: " + sum);
See both under 5 below.
for (int k = 10; k >= 1; k--) {
System.out.print(k);
if (k != 1) System.out.print(", ");
}
System.out.println();
| Original Loops | Altered Loops |
|---|---|
public class Loops1 {
public static void main(String[] args) {
// change to for
int i = 1;
int sum = 0;
while (i < 10) {
sum = sum + i*i;
i++;
}
System.out.println("sum: " + sum);
// change to while
for (int k = 10; k >= 1; k--) {
System.out.print(k);
if (k != 1) System.out.print(", ");
}
System.out.println();
}
}
| public class Loops2 {
public static void main(String[] args) {
// the for loop
int sum = 0;
for (int i = 1; i < 10; i++) {
sum = sum + i*i;
}
System.out.println("sum: " + sum);
// the while loop
int k = 10;
while (k >= 1) {
System.out.print(k);
if (k != 1) System.out.print(", ");
k--;
}
System.out.println();
}
}
|
| Common Output | |
sum: 285 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 | |
1 + 1/22 + 1/32 + 1/42 + 1/52 + ...
| File: Pi.java | File: PiTest.java |
|---|---|
public class Pi {
private int numTerms;
public Pi(int terms) {
numTerms = terms;
}
public double calcPi() {
double sum = 0.0;
for (int i = 1; i <= numTerms; i++) {
sum = sum + 1.0/i/i;
}
return sum;
}
}
// Note: we could have written
// sum = sum + 1.0/((double)i*i);
// but NOT
// sum = sum + 1.0/(i*i); (WHY?)
| import java.util.Scanner;
public class PiTest {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Type an int for # terms ---> ");
int terms = in.nextInt();
Pi pi = new Pi(terms);
double piApprox = pi.calcPi();
System.out.println("Sum of series (approx): " +
piApprox + ", with " + terms + " terms");
System.out.println("Sum (exact value): " +
Math.PI*Math.PI/6.0);
System.out.println("Pi from series (approx): " +
Math.sqrt(piApprox*6.0) + ", with " +
terms + " terms");
System.out.println("Pi (exact value): " +
Math.PI);
}
}
|
| Output of 3 Runs | |
Type an int for # terms ---> 10000 Sum of series (approx): 1.6448340718480652, with 10000 terms Sum (exact value): 1.6449340668482264 Pi from series (approx): 3.1414971639472147, with 10000 terms Pi (exact value): 3.141592653589793 Type an int for # terms ---> 100000 Sum of series (approx): 1.6449240668982423, with 100000 terms Sum (exact value): 1.6449340668482264 Pi from series (approx): 3.141583104326456, with 100000 terms Pi (exact value): 3.141592653589793 Type an int for # terms ---> 1000000 Sum of series (approx): 1.64493306684877, with 1000000 terms Sum (exact value): 1.6449340668482264 Pi from series (approx): 3.1415916986605086, with 1000000 terms Pi (exact value): 3.141592653589793 | |
| First Data | Second Data |
|---|---|
public class Comp {
public static void main(String[] args) {
double amount = 1000;
int years = 4;
double rate = 10.0;
for(int i = 1; i <= years; i++) {
amount = amount + amount*rate/100.0;
System.out.print("End of year " + i);
System.out.println(", amount: " +
amount);
}
System.out.println();
}
}
| public class Comp {
public static void main(String[] args) {
double amount = 1000;
int years = 6;
double rate = 30.0;
for(int i = 1; i <= years; i++) {
amount = amount + amount*rate/100.0;
System.out.print("End of year " + i);
System.out.println(", amount: " +
amount);
}
System.out.println();
}
}
|
| First run | Second run |
End of year 1, amount: 1100.0 End of year 2, amount: 1210.0 End of year 3, amount: 1331.0 End of year 4, amount: 1464.1 | End of year 1, amount: 1300.0 End of year 2, amount: 1690.0 End of year 3, amount: 2197.0 End of year 4, amount: 2856.1 End of year 5, amount: 3712.93 End of year 6, amount: 4826.809 |
For a solution to the full problem, see Compound interest.