Monthly Archives: November 2020

Built In: Using Scanner for input


/**
 *  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

Built-In: Random Numbers + Scanner Class

Screen Shot 2014-09-09 at 10.24.34 PM

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

Screen Shot 2014-09-09 at 10.24.34 PM

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

Screen Shot 2014-09-09 at 10.24.34 PM

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.

      1. 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);
        
      2. Give an example for each of the following type conversions and include a comment to indicate how the conversion takes place.
        1. Explicit type conversion
        2. Automatic type conversion
        3. Explicit casts
        4. Automatic conversions for strings
      3. Write a line of code to divide a variable num of type double by zero. What is the output?
      4. What do each of the following print? Explain each outcome.
        1. System.out.println(2 + “bc”); prints: 2bc
        2. System.out.println(2 + 3 + “bc”); prints: 5bc
        3. System.out.println((2+3) + “bc”); prints: 5bc
        4. System.out.println(“bc” + (2+3)); prints: bc5
        5. System.out.println(“bc” + 2 + 3); prints: bc23
      5. Explain how to use Quadratic.java to find the square root of a number.
      6. What do each of the following print?
        1. System.out.println(‘b’);
        2. System.out.println(‘b’ + ‘c’);
        3. System.out.println((char) (‘a’ + 4));
      7. 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.
        1. System.out.println(a);
        2. System.out.println(a + 1);
        3. System.out.println(2 – a);
        4. System.out.println(-2 – a);
        5. System.out.println(2 * a);
        6. System.out.println(4 * a);

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))

  1. 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

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 π.

screen-shot-2016-09-20-at-9-01-32-am

Built-in: Boolean Algebra

 

Some basic concepts from MathHacks:

 

Built-in: Review and Notes

Screen Shot 2014-09-09 at 10.24.34 PM

Review and quick notes:

  1. What is the difference between compiling and running a program?
  2. Name 5 common built-in types also known as primitive data types.
  3. What are the differences and similarities between char and Strings?
  4. What java type is used to represent a Real Number?
  5. Draw the truth table for && and || for two boolean variables a and b.
  6. 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
  7. Write a code snippet to determine if a number is even.
  8. How do you display a quote as part of a string?
  9. How do you display a backslash as part of a string?
  10. When all the operations in an expression have the same precedence how does the java compiler evaluates the expression?
  11. What does each of the following print? Explain
    1. System.out.println(2 + "bc"); prints: 2bc
    2. System.out.println(2 + 3 + "bc"); prints: 5bc
    3. System.out.println((2+3) + "bc"); prints: 5bc
    4. System.out.println("bc" + (2+3)); prints: bc5
    5. System.out.println("bc" + 2 + 3); prints: bc23
  12. 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