CSCI 470/680E -- Assignment 1 - Fall 2009


Step 1:

Java "Hello, World"

This first part of the assignment is designed to familiarize you with the mechanics of creating, compiling, and running a text-mode Java application. You do not have to hand this in.

You can use the JDK on the Linux host turning.cs.niu.edu or the JDK on Windows XP in the labs. 

For turing.cs.niu.edu, the path the Java compiler and Java Virtual Machine (JVM) is

/usr/bin

and the Windows path in lab machines can be found via the Lab menu for Java.

In both environments, you should "be in" the directory where your Java program (source and class files) are stored, and you should invoke the compiler or JVM using the fully qualified path name.  So for example,

/usr/bin/javac Add.java
c:\...\bin\javac Add.java   (where ...\bin is the path of the java compiler)

The source code below simply prompts for and accepts two numbers from the user, adds them, and displays the result.  This is supplied so you can go through the mechanics of editing, compiling and running a console-based Java program.  You will not hand it in, but you should write and run it.

Note: The file name, Add.java is case-sensitive and must match the class name in the program below.

Details of the code will be covered in class after a week or two.

/****************************************************************
Program to add two numbers... note that input is accepted as a 
String and then an attempt is made to convert it to a double for
calculations.  Non-numeric input is detected by the Exception
mechanism and a default value is assigned to the value.
More complete documentation should be supplied here...
*****************************************************************/
import java.io.*;
import java.util.Scanner;

public class Add
  {
  //--------------------------------------	
  public static void main(String args[])
    {
    String amtStr;
    double num1  = 0.0,
           num2  = 0.0,
           tot   = 0.0;
        
    Scanner sc = new Scanner(System.in);        
    System.out.println("Enter the first number: ");
    amtStr = sc.next();
    // try to convert amt String to double for calculation    
    try
      {
      num1 = new Double(amtStr).doubleValue();
      }
    catch (NumberFormatException e)
      {
      System.out.println("Bad numeric input; 1st num set to 100");
      num1  = 100;
      }
    System.out.println("Enter the second number: ");
    amtStr = sc.next();
    try
      {
      num2 = new Double(amtStr).doubleValue();
      }
    catch (NumberFormatException e)
      {
      System.out.println("Bad numeric input; 2nd num is set to 50");
      num2 = 50;
      }
    tot = num1 + num2;
            
    System.out.println("Sum is: " + tot);
    }  // end main
  } // end of class Add

Step 2:

Based on techniques shown above and your knowledge of C++ (and its similarity to Java), write a program to analyze quadratic equations. It will find the roots of the equation (if any) and will also calculate and display the x- and y-coordinates of the vertex of the parabola represented by the equation and whether the curve is concave upwards or concave downwards.   It will then ask the user if another set of calculations should be performed.

A typical single run of the program should resemble the following:

Quadratic Equation Analyzer
Enter the a coeficient: 1
Enter the b coefficient: 2
Enter the c coefficient: -2
The parabola opens upwards.
The coordinates of the vertex are x = xx.xxx and y = zz.zzz
The parabola has 2 roots at 
  x = nn.nnn  
  x = aa.aaa 
Another? (y/n) n

Recall that the general quadratic equation is: 
The formula for the roots of a quadratic equation is:
The x-coordinate of the vertex is at:
The y-coordinate of the vertex is at:

Recall also that a quadratic equation is in the shape of a parabola. The parabola opens upward if the coefficient of x-squared (a in the equations above) is positive and it opens downward if it is negative. The coefficient must be non-zero (if it is zero, the equation is of a straight line). The other constants (b and c) can be any real values.

The roots of the equation are the places where the parabola cuts the x-axis. If the parabola is entirely above or below the x-axis, it has no real roots. If it just touches the x-axis, it has one root with a multiplicity of 2. Otherwise, it cuts the axis at 2 points.

You can determine which of these cases holds for a given equation by checking the value of the discriminant (the quantity under the square root sign). If it is positive, there are two real roots; if it is 0 there is one root (multiplicity of 2) and if it is negative, there are no real roots.

After prompting the user for a, b, and c (see the methods you are to write, described below), the program should then determine and display

The program should then ask if the user wishes to do another equation. Accept either "y" or "Y" as positive responses.

When your program starts it should display a title on the screen. All prompts should be clear and accurate. All calculated results should be presented with clear and accurate labels. Print results one item per line. Print real number results to 3 decimal places. Test your program for all possible cases before you conclude it is finished and correct. Use equations of parabolas whose roots, concavity, and vertex location you know.

Note: you can use the Math.sqrt() method.  sqrt() is a public static method of the Math class. This syntax will be explained in lecture. Here's a sample use:

x = Math.sqrt(y);

where x and y are double variables.  The square root of y is stored into x here.


Methods

You should write the following methods and call them properly from the appropriate place(s) in main().  Note also that in a couple of places, code in one of your new methods will call another of these new methods.

char getYesNo(string s) - this method will use the argument string s to prompt the user.  When you call this method from main(), just supply the desired prompt as a string literal:

choice = getYesNo("Another? (y/n) ");   // choice is a char variable

Then the method will accept a string from the user.  Then,

The method will not return until the user has entered one of the 10 acceptable choices.  (This way the caller will not have to test all the possibilities: just 'y' or 'n'.)

double getA() - this method will prompt the user to enter the value of the a-coefficient.  It will check to see if it is 0.  If it is, it will display an error message and prompt for a non-zero value.  It will not return until the user has entered a non-zero value; it will then return that non-zero value. Note that since any  real values are valid for b and c, there's little point in writing special methods to get these values.  Just use a prompt and accept a value Use the Exception pattern from Step 1 to catch invalid numbers and ask the user to re-enter the value.

double getDiscr(double a, double b, double c) - this method will calculate and return the value of the discriminant.  a, b, and c are the coefficients that were entered by the user.

double getVertex_X(...) and double getVertex_Y(...) will calculate and return the x and the y coordinates of the vertex.  You must determine what arguments are needed.

double getRoot1(...) and double getRoot2(...) will each return one of the equation's roots. (One with the + and one with the -) You must determine what arguments are needed.   You must also determine (based on the sign of the discriminant) which to call:


Numeric Output Formatting

Floating point numeric output from System.out.println() will display decimal places - but you can't control how many.  You can control the number of decimal places displayed by making use of a special Java class.  Do the following:

1. import java.text.DecimalFormat;

2. Create a DecimalFormat object as follows:

DecimalFormat df = new DecimalFormat("#.000");  // for 3 decimal places

3. When you want to display a number, pass it to the format() method of this object:

System.out.println("Number is " + df.format(adouble));

The format() method takes a double and returns a String, which is the text representation of the double's value, formatted as specified in the constructor of the DecimalFormat object.  Think about this if you need to until you understand it.


Assignment Submission

Hand in printed source code on the due date (which will be announced) in lecture.  Also, post the source code on the course Blackboard site by the due date.