Category Archives: Homework

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: drawing in a piece of paper – Privacy violations

Classwork:
Visit edmodo.com
Create and account or join with the code for your period.
Find the code on the home page.
Please use your full name as your screen name.

Designing an algorithm

Find a partner. You will need paper and pencil/pen.
Activity 1:

Your teacher will hand you a drawing in a piece of paper.

  • Don’t let your partner see the drawing.
  • Your partner will draw the same shape on his/her paper by following your “set of instructions”.
  • Write down the instructions as you give them.
  • You can not use direct words that describes the image.
  • Once finished, both look at the drawings and compare them.
  • If the drawings don’t match, go back to your instructions and change them until you are satisfied.
  • You and your partner reverse roles.
  • Do the instructions need to be revised?

Homework:

Read the following articles linked bellow and answer questions on edmodo.com
Privacy violations – the dark side of social media
Screen Shot 2014-09-04 at 6.12.33 PM

Digital Explosion: Why Is It Happening, and What Is at Stake?
Screen Shot 2014-09-04 at 6.27.26 PM

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