/** * good resource: http://docs.oracle.com/javase/tutorial/essential/io/scanning.html * @author gracielaelia * Problem: Both hasNext and next methods may block waiting for further input. * hasNext() doesn't return false ever!!!! next() blocks until something is input! * As you're reading from stdin, that'll either be when you send an EOF character (usually ^d on Unix), * or at the end of the file if you use < style redirection. * You cannot easily send and EOF character using the keyboard" * -- usually Ctrl+D on Linux/Unix/Mac * or Ctrl+Z on Windows does it */ import java.util.Scanner; public class ScannerTest_GE { public static void main(String[] args){ Scanner scan = new Scanner(System.in); String name; // declaration double base, exponent; System.out.println("Enter the base "); base = scan.nextDouble(); System.out.println("Enter the exponent "); exponent = scan.nextDouble(); System.out.println("Enter your name " ); name = scan.next(); System.out.println(name + " the value is "+ Math.pow(base,exponent)); System.out.println(name + " the integer value is "+ (int)Math.pow(base,exponent)); } } // Enter the base // 2 // Enter the exponent // 5 // Enter your name // grace // The value is 32.0 // The integer value is 32
Monthly Archives: November 2020
Built-In: Random Numbers + Scanner Class
Classwork:
Write a short program to understand how the Math.random() method works.
NOTE: double r = Math.random();
Resources for some of these assignments:
[spoiler title=’Math.random()’]/** * An itty bitty program to learn about Math.random() * GE * 9/28/2017 * */ public class MathRandTest_GE { public static void main(String []args) { for (int i = 0; i < 100; i++) { System.out.println(Math.random()); } } }[/spoiler]
Be original and create your own "random" program, MyOriginalRand_YI.java using Math.random().
[spoiler title='Scanner Class']/** * good resource: http://docs.oracle.com/javase/tutorial/essential/io/scanning.html * @author gracielaelia * Problem: Both hasNext and next methods may block waiting for further input. * hasNext() doesn't return false ever!!!! next() blocks until something is input! * As you're reading from stdin, that'll either be when you send an EOF character (usually ^d on Unix), * or at the end of the file if you use < style redirection. * You cannot easily send and EOF character using the keyboard" * -- usually Ctrl+D on Linux/Unix/Mac * or Ctrl+Z on Windows does it */ import java.util.Scanner; public class ScannerTest_GE{ public static void main(String [] args) { Scanner scan = new Scanner(System.in); String name; // just a declaration double base, exponent; System.out.println("Enter the base "); base = scan.nextDouble(); System.out.println("Enter the exponent "); exponent = scan.nextDouble(); System.out.println("Enter your name "); name = scan.next(); System.out.println(name + " the value is " + Math.pow(base,exponent)); System.out.println(name + " the integer value is " + (int)Math.pow(base,exponent)); } }[/spoiler]
Be original and create your own test program, MyOriginalScanner_YI.java using the Scanner class to prompt the user for input to do "something original".
Built-in: LoanPayments.java
Classwork:
Random Number Generator
Loan payments. Write a program, LoanPayments_YI.java
that calculates the monthly payments you would have to make over a given number of years to pay off a loan at a given interest rate compounded continuously, taking the number of years t, the principal P, and the annual interest rate r as command-line arguments. The desired value is given by the formula Pe^(rt). Use Math.exp().
Built-in Ex: Types Conversions
Classwork:
Built-In Exercises – Type conversions and more
For this assignment write a java program, BuiltIn1_YI.java to produce the code necessary to answer all the questions clearly.
-
-
- In which way are the variables “x” and “a” alike in the following code snippet?
int x; char a = 'z'; System.out.println((int)a); x = 't'; System.out.println(x);
- Give an example for each of the following type conversions and include a comment to indicate how the conversion takes place.
- Explicit type conversion
- Automatic type conversion
- Explicit casts
- Automatic conversions for strings
- Write a line of code to divide a variable num of type double by zero. What is the output?
- What do each of the following print? Explain each outcome.
- System.out.println(2 + “bc”); prints: 2bc
- System.out.println(2 + 3 + “bc”); prints: 5bc
- System.out.println((2+3) + “bc”); prints: 5bc
- System.out.println(“bc” + (2+3)); prints: bc5
- System.out.println(“bc” + 2 + 3); prints: bc23
- Explain how to use Quadratic.java to find the square root of a number.
- What do each of the following print?
- System.out.println(‘b’);
- System.out.println(‘b’ + ‘c’);
- System.out.println((char) (‘a’ + 4));
- Suppose that a variable a is declared as int a = 2147483647 (or equivalently, Integer.MAX_VALUE). What do each of the following print? Explain each outcome.
- System.out.println(a);
- System.out.println(a + 1);
- System.out.println(2 – a);
- System.out.println(-2 – a);
- System.out.println(2 * a);
- System.out.println(4 * a);
- In which way are the variables “x” and “a” alike in the following code snippet?
-
Built-in Ex: Trig, Booleans and Xor
Built-in Ex 7 and 8:
7a. Write a program that uses Math.sin() and Math.cos() to check that the value of sin^2(θ) + cos^2(θ) is approximately 1 for any θ entered as a command-line argument. Just print the value. Why are the values not always exactly 1?
7b. Suppose that a and b are boolean values. Simplify the following expression: (!(a && b))
- The exclusive or operator ^ for boolean operands is defined to be true if they are different, false if they are the same. Give a truth table for this function
Blackboard: Tracing 101: Three Numbers
Tracing 101: ThreeNums.java
ThreeNums
Built-in Ex: Polar Coordinates
Polar coordinates.
Write a program CartesianToPolar_YI.java that converts from Cartesian to polar coordinates. Your program should take two real numbers x and y on the command line and print the polar coordinates r and θ. Use the Java method Math.atan2(y, x), which computes the arctangent value of y/x that is in the range from -π to π.
Built-in: Boolean – Truth table & Math Library
Built-in: Boolean Algebra
Built-in: Review and Notes
Review and quick notes:
- What is the difference between compiling and running a program?
- Name 5 common built-in types also known as primitive data types.
- What are the differences and similarities between char and Strings?
- What java type is used to represent a Real Number?
- Draw the truth table for && and || for two boolean variables a and b.
- Show the truth table for the following expression:
!( p && ( q || !r))
for the following values of p, q, and r
f f f
f f t
f t f
f t t
t f f
t f t - Write a code snippet to determine if a number is even.
- How do you display a quote as part of a string?
- How do you display a backslash as part of a string?
- When all the operations in an expression have the same precedence how does the java compiler evaluates the expression?
- What does each of the following print? Explain
- System.out.println(2 + "bc"); prints: 2bc
- System.out.println(2 + 3 + "bc"); prints: 5bc
- System.out.println((2+3) + "bc"); prints: 5bc
- System.out.println("bc" + (2+3)); prints: bc5
- System.out.println("bc" + 2 + 3); prints: bc23
- What is the value of Integer.MAX_VALUE?
Some Help
#5
a b a && b a || b
f f
t f
f t
t t
#6
p q r q || !r !( p && ( q || !r))
f f f
f f t
f t f
f t t
t f f
t f t