Personal tools
You are here: Home Comp 170 Introduction to OOP Lab Materials Lab14 QuadInJava
Document Actions

QuadInJava

by Mit Patel last modified 2006-04-27 12:45 PM

Helper file for Quad for those who want a starting point otherwise, most welcome to start from scratch.

Click here to get the file

Size 2.1 kB - File type text/x-java

File contents

import java.util.Scanner; 
 
public class Quad 
{ 
   //----------------------------------------------------------------- 
   //  Skeleton of quad class. 
   //  I only show you guys three exceptions in case you don't know
   //  quadratic rules.  
   //  Think of what else throws an expection and catch them in your
   //  project.  These should be shown to us when you execute the 
   //  program.  I can think of at least one very obvious one.
   //----------------------------------------------------------------- 
   public static void main (String[] args) 
   { 
      int a, b, c;  // ax^2 + bx + c
  
      double discriminant, root1, root2; 
      boolean restart;
      
      //User input three integers.
      Scanner scan = new Scanner (System.in); 

do {      
      System.out.print ("Enter the coefficient of x squared: "); 
      a = scan.nextInt(); 
 
      System.out.print ("Enter the coefficient of x: "); 
      b = scan.nextInt(); 
 
      System.out.print ("Enter the constant: "); 
      c = scan.nextInt(); 
   
 //should use try and catch
     
      //if 'a' is zero then throw an exception saying 
      //a can't be zero
     
      //else go on to compute discriminat and roots------------
      
      //dicriminant, compute discriminant.  Can use pow from math class
   
      //if discriminant is zero than throw an exception saying 
      //discriminant is less than zero.
      
      //two roots, compute two roots.  Can use sqrt function from 
      //math class
 
      System.out.println ("Root #1: " + root1); 
      System.out.println ("Root #2: " + root2); 
      //end of else--------------------------------------------

 //end of try---------------

 
    //catch an exception if there is no solution

//--------------------------------------------------------------------

      //program continues if user enters true and exits if entered false.   
      System.out.println();
      System.out.println("True to continue and False to exit");
      restart = scan.nextBoolean();
  } while(restart);
    
   
    } //end of main
} //end of class
« June 2022 »
Su Mo Tu We Th Fr Sa
1234
567891011
12131415161718
19202122232425
2627282930
 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: