Author Archives: graceliana@gmail.com

Built-in: Distance Calculation and Magic Numbers

 
 

Write a program Distance_YI.java with these values x1 = 3.0, x2 = 5.0, y1 = 2.5 and y2 = -1.9 and prints the Euclidean distance from the point (x1, y1) to the origin (x2,y2). Your program should display the numbers from each point with exactly the same format, meaning include parenthesis and comma and the distance as one number in a sentence format. This is what it should look like:

“The distance between points (3.0,5.0) and (2.5,-1.9) is 4.8332”


Resources:

Math.sqrt(4) —> 2.0 Math.pow(base,exponent);

Example: Math.pow(2,5) —> 32

Use the following data type and make sure all your code is inside the main method:

double x1 = 3.0;


NOTE: do not use numbers. Use variables. Using numbers is called “MAGIC NUMBERS”
“MAGIC NUMBERS” are consider bad programming practice.

 

In this image and in the video you will see how “constants” are represented and used.

Built-in: Sum of 2 random int

Write a program SumOfTwoRand_YI.java that prints the sum of two random integers between 1 and 6 (such as you might get when rolling dice).
As a good recommendation, first write the program to understand how the Math.random() method works.


NOTE: double r = Math.random();

Warning: Do not use Math.round(). Instead use the casing operator (int)

Built-in Ex: Day of the Week

Day of the week.
Write a program DayOfWeek_YI.java that takes a date as input and prints the day of the week that date falls on. Your program should take three command-line arguments: m (month), d (day), and y (year). For m use 1 for January, 2 for February, and so forth. For output print 0 for Sunday, 1 for Monday, 2 for Tuesday, and so forth. Use the following formulas, for the Gregorian calendar (where / denotes integer division):

Screen Shot 2015-09-29 at 3.07.19 PM

screen-shot-2016-09-20-at-8-49-10-am

For example, on which day of the week was August 2, 1953?

dow-example

Visit edmodo.com to check due dates and to submit your work.

Built-in: Quadratic Equation

Write a program, QuadraticEQ_YI.java that takes as input arguments the coefficients of the quadratic equation, a, b, c and finds the solutions of the equation. In other words, if a, b and c are the coefficients of ax^2 + bx + c, then a can be obtained with args[0], b with args[1] and c with args[2]


NOTE: Assume the equation has two solutions.

Below is the syntax highlighted version of Quadratic.java from §1.2 Built-in Types of Data.
/******************************************************************************
 *  Compilation:  javac Quadratic.java
 *  Execution:    java Quadatic b c
 *  
 *  Given b and c, solves for the roots of x*x + b*x + c.
 *  Assumes both roots are real valued.
 *
 *  % java Quadratic -3.0 2.0
 *  2.0
 *  1.0
 *
 *  % java Quadratic -1.0 -1.0
 *  1.618033988749895
 *  -0.6180339887498949
 *
 *  Remark:  1.6180339... is the golden ratio.
 *
 *  % java Quadratic 1.0 1.0
 *  NaN
 *  NaN
 *
 *
 ******************************************************************************/

public class Quadratic { 

    public static void main(String[] args) { 
        double b = Double.parseDouble(args[0]);
        double c = Double.parseDouble(args[1]);

        double discriminant = b*b - 4.0*c;
        double sqroot =  Math.sqrt(discriminant);

        double root1 = (-b + sqroot) / 2.0;
        double root2 = (-b - sqroot) / 2.0;

        System.out.println(root1);
        System.out.println(root2);
    }
}
Copyright © 2000–2017, Robert Sedgewick and Kevin Wayne. Last updated: Fri Oct 20 14:12:12 EDT 2017.

Built-in: Range between 2 nums – Scanner

Write a program, RangeBetween2_YI.java to prompt the user for two integers. One should be the min and the other one the max. The next prompt should ask for the quantity of random numbers to display within the specified range. Include a message at the start and at the end of the program.

NOTE: Do not use if or if/else statements.
Recommendation: look at these previous programs
SumOfTwoRand_YI.java for the random component where you simulate rolling two dices and then find the sum of the face value of the two of them.
ScannerTest_YI.java how to use the “for loop”.

 

/** 
* An itty bitty program to learn about Math.random()
* and a very useful tool: the for-loop
* GE
* 9/28
*
*/
public class MathRandTest_GE
{
   public static void main(String []args)
   {
      for (int i = 0; i < 50; i++)
         {
           System.out.println((int)(Math.random() * 6 ) + 1);
         }
    }
}
Let's understand Math.random()

Math.random() 0 ----> 0.99999999

((int)(Math.random() * 6 ) + 1)

Let's take any two random numbers from the random() function, 0.5 and 0.9

0.5 * 6 = 3.0
0.9 * 6 = 5.4

(Math.random() * 6 ) ---> 5.999… < 6 
(int)(Math.random() * 6 ) ---> 5
5 + 1 ----> 6

side note:
(int)6.999…. ----> 6 // the (int) truncates the decimal part.

 

Recall the 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;  // 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: 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".