Monthly Archives: November 2020

CS Tools: Practice-it!

     click here     

Practice-It is a web application to help you practice solving Java programming problems online.

To use Practice-It, first create an account, then choose a problem from our list. Type a solution and submit it to our server. The system will test it and tell you whether your solution is correct.

Marty Stepp, of Stanford University Computer Science, is the primary developer of Practice-It, although Practice-It is an independent tool not directly affiliated with Stanford University nor any other university. Marty runs a similar site, CodeStepByStep.com, that offers a different selection of coding problems for Java, C++ and Python.

Current contributors to Practice-It also include:

  • Melissa Galloway, University of Washington
  • Ryan Rowe, University of Washington
  • Whitaker Brand, University of Washington

Some notable past contributors to the development and administration of Practice-It are:

  • Katlyn Edwards
  • Zack Cava
  • Jessica Miller

Some former students and TAs who have authored or ported a significant number of problems into Practice-It include:

  • Roy McElmurry
  • Robert Baxter
  • Leslie Ferguson

Also thanks to:

  • Zorah Fung for drawing the Practice-It logo image and mascot
  • Glen Herrmannsfeldt from Caltech for contacting us with many bug fixes and suggestions for improvement to the quality of the system
  • Countless other teaching assistants and colleagues who added problems

Practice-It is inspired by similar tools such as CodingBatJavaBall, and CodeWrite.

Our Privacy Policy

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