Category Archives: Uncategorized

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



 

Advanced Topic: Static Identifier

The Static Specifier

foxtrot.100.in.binary

NOTE: use the implementation already written from the previous assignment but pay attention to the changes.
(The YI_MyStaticInteger class) Design a class named MyInteger. The class contains:
■ A static int data field named intCount that stores the number of object of this class created in the driver.
■ An int data field named value that stores the int value represented by this object.
■ A constructor that creates a MyStaticInteger object for the specified int value and updates the intCount data field.
■ A get method that returns the int value.
■ A get method that returns the int intCount.
■ The methods isEven(), isOdd(), and isPrime() that return true if the value in this object is even, odd, or prime, respectively.
■ The static methods isSEven(), isSOdd(), and isSPrime() that return true if the value in this object is even, odd, or prime, respectively. Comment out the code that causes any problem to be able to continue successfully with the implementation.
■ Compare the last two methods behaviors and explain the differences.
■ Report any problems and messages as comments and explain what the problem is. If you can’t figure it out, watch yesterday’s video again.
■ The static method getintCount() should be part of the class and used in the driver to display the number of object of MyStaticInteger class.

Input/Output: Graphics: Histogram – StdDraw

Screen Shot 2015-02-23 at 12.19.21 AM

Classwork:
Standard Drawing
histogram
Suppose that the standard input stream is a sequence of double values. Write a program that takes an integer N and two double values left and right from the command line, StdIn, or Scanner and uses StdDraw to plot a histogram of the frequency of the numbers in the standard input stream that fall in each of the N intervals defined by dividing (left , right) into N equal-sized intervals.
The intervals’ marks and labels are optional.
Assumption: data values range from 0 to 100
Extra challenge: no range of values is known.

For development purpose ONLY, you can hard code the data or have a text file with the data. Make sure you have at least 30 data values with a good amount of variation in the values. Take a screenshot and turn in both the image and the data values.

Suggestion:
– Start by drawing a rectangle given the width and height as input.
– Next, draw n number of rectangles given the width, both n and width as input. For learning purposes, use a sample array of integers representing the frequency (the height of each rectangle) at each position.
– Last, replace the array of integers with the frequency of integers from the input stream based on the above specification. At this point you can add to your implementation the other constraints, left, right and N.

Homework:
Complete missing assignments.

Input/Output: Graphics: Hex. Hex is a two-player board game

Animations using java
duke

Screen Shot 2015-02-23 at 12.19.21 AM

NOTE: INCLUDE GOOD DOCUMENTATION IN YOUR PROGRAMS
Classwork:
Standard Drawing: Animation and images Images.

  1. Hex. Hex is a two-player board game popularized by John Nash while a graduate student at Princeton University, and later commercialized by Parker Brothers. It is played on a hexagonal grid in the shape of an 11-by-11 diamond Write a program Hex.java that draws the board.

Screen Shot 2016-01-06 at 6.27.23 PM