/* CIS 790 JAVA PROGRAMMING PROF. AUCIELLO
input-formatting-output-functions
Topics:
1. data types (int, float, etc)
2. "+" as catenator
3. inputting variables
4. System.out.println()
-- prints to standard system output
screen, log file, etc.
Java Integer Types:
byte 8 bits
short 16 bits
int 32 bits
long 64 bits
types of variables to be read:
readInt int
readDouble float
readString string
readWord reads string until a space is encountered
OUTPUT:
C:\CoreJavaBook>java javapr03
Enter Integer (no dec. points) for x: 15
Enter Integer value for y: 4
x is 15, y is 4
x + y = 19
x - y = 11
x / y = 3
x % y = 3
x * y = 60
Enter Floating Point Number (has dec. points) for a: 3.5
Enter FP Number for b: 2.5
Unformatted Output:
a is 3.5, b is 2.5
a / b = 1.4
a raised to b = 22.91765149399039
Output using Format.print
a is 3.50 b is 2.50
a / b = 1.40
a raised to b = 22.92
*/
import corejava.*;
class javapr03
{ public static void main (String args [])
{ int x, y;
double a, b;
x = Console.readInt("Enter Integer (no dec. points) for x: ");
y = Console.readInt("Enter Integer value for y: ");
System.out.println ("x is " + x + ", y is " + y);
System.out.println ("x + y = " + (x + y));
System.out.println ("x - y = " + (x - y));
System.out.println ("x / y = " + (x / y));
System.out.println ("x % y = " + (x % y));
System.out.println ("x * y = " + (x * y));
System.out.println (" ");
System.out.println (" ");
a = Console.readDouble("Enter Floating Point Number (has dec. points) for a: ");
b = Console.readDouble("Enter FP Number for b: ");
System.out.println ("Unformatted Output: ");
System.out.println ("a is " + a + ", b is " + b);
System.out.println ("a / b = " + (a / b));
System.out.println ("a raised to b = " + Math.pow(a,b));
System.out.println (" ");
System.out.println ("Output using Format.print");
Format.print(System.out, "a is %5.2f", a);
Format.print(System.out, " b is %5.2f", b);
System.out.println (" ");
System.out.print ("a / b = ");
double c = a/b;
Format.print(System.out, " %5.2f",c);
System.out.println (" ");
System.out.print ("a raised to b = ");
// XX Format.print(System.out, " %5.2f", ____________);
System.out.println (" ");
}
}
/*
// Note: standard output = System.out.println
/* Questions: What happened to the decimals points in the
results using integers?
Define % function -- tell instructor when you
figure it out.
Does 'Format.print' round off at 5 or greater?
Math.pow(x,y) returns a result of x raised to the y power.
Insert correct code in line marked "XX" above.
Q1: can "public" in 'public class' be removed?
public static void main(String[] args)
public static void main(String args[])
Q2: Which one works? Try both. write answer.
Q3: explain int x,y
double a,b
Q4: explain the line below. What 2 actions occur?
years = Console.readInt ("The number of years: ");
Q5: Is Math.pow is a library method. (routine already coded).
Format.print(System.out," With rate %6.3f", 100*y);
Format.print(System.out," your payment is $%10.2f", payment);
System.out.println(" ");
Q6: explain "%6.3f" and 100 * y
Q7: explain "$%10.2f" (note "edit mask" is within literal string.)
Q8: why is "System.out.println(" ")" is necessary.
*/
|