Category Archives: Classwork

First: Your programs format

Screen Shot 2014-09-07 at 9.29.42 PM

Your programs format:
1. Make sure you have a header: Author’s name, date and description (assignment)
2. Type the code from the class website.
3. Name your file as YI_HelloWorld (YI means your initials)
4. As comments enter your input.
5. Run it.
6. As comments enter your output.
7. Copy and paste your program here.
8. Attached the “.java” file.

Errors

  • Compile-time errors
  • Run-time errors
  • Logical errors

Q + A

Homework:
Web Exercise 1-4

First: Java Program Syntax

First Programs – Creating your first program …

1st. Create a folder “Java2019_PX” in your Documents folder.
2nd. Create a folder “Unit1” in your “Java2019_PX” folder.
3rd. Create a google doc “JavaIntroNotes_YI” in your drive and share it with gracielaelia@pps.princetonk12.org.
4th. Back up your work to the drive of your choice or use edmodo.com Backpack feature.

First Programs – Ex 1- 5 Basic Java Programs

1. Write a program TenHelloWorlds.java that prints “Hello, World” ten times.

2. Describe what happens if, in HelloWorld.java, you omit
public
static
void
args

3. Describe what happens if, in HelloWorld.java, you omit

a. the ;
b. the first ”
c. the second ”
d. the first {
e. the second {
f. the first }
g. the second }

4. Describe what happens if you try to execute Hi.java with:
Hi.java:

public static void main(String [] args)
{
System.out.println(“Hello ” + args0);
}

In terminal or BlueJ
Run these lines in terminal/command line.

java Hi
java Hi @!&^%
java Hi 1234
java Hi.class Bob
java Hi.java Bob
java Hi Alice Bob

5. Modify UseArgument.java to make a program UseThree.java that takes three names and prints out a proper sentence with the names in the reverse of the order given, so that for example, “java UseThree Alice Bob Carol” gives “Hi Carol, Bob, and Alice.”.

Please take notes and share your document with me.
Use mrseliaphs@gmail.com to share your notes.
If you need/want to email me for any reason, use gracielaelia@princetonk12.org

edmodo.com – First Programs – Ex 1- 5 Basic Java Programs

First: Connecting Related Objects

1. Form a group of students and create your own graph with related objects. Include your initials on the line connecting the objects.

2. For each item make a list of all the objects that are directly connected to it.

3. Submit to edmodo.com the following items:
a. The name of your team mates.
b. A picture of your graph.
c. The list you created in part 2.

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: 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().