Pages

Total Pageviews

Friday, June 14, 2019

Lab 7 Exercise Questions at Java Programming

import java.util.Scanner;
   class Quadratic   // It is a code that describes the solution of operations involving quadratic equations.
{
    public static void main(String[] args)
     {
        double A, B, C;                   // We enter variable definitions into the system.
        double root1, root2, d;
        Scanner s = new Scanner(System.in);  // The scanner class has methods that will take a typed in string and convert it into the data types that we need.
     
        boolean inputFails = true;
     
         while(inputFails) {
            try {
                 System.out.println("Given quadratic equation:A*x^2 + B*x + C");    // After entering the quadratic equation into our system, we define the values integer.
                 System.out.print("Enter A:");
                 A = s.nextDouble();
                 if(A == 0)
                 {
                     throw new Exception("Value of A cannot be zero");
                 }
               
                 System.out.print("Enter B:");       
                 B = s.nextDouble();
                 System.out.print("Enter C:");
                 C = s.nextDouble();
                 System.out.println("Given quadratic equation:"+A+"*x^2 + "+B+"*x + "+C); // In this section, integers are entered to find the desired result in the given formula values.
                 d = B * B - 4 * A * C;
               
               
                 if(d > 0)
                 {
                     System.out.println("Roots are real and unequal");
                     root1 = ( - B + Math.sqrt(d))/(2*A);
                     root2 = (-B - Math.sqrt(d))/(2*A);
                     System.out.println("First root is:"+root1);
                     System.out.println("Second root is:"+root2);
                 }
                 else if(d == 0)
                 {
                     System.out.println("Roots are real and equal");
                     root1 = (-B+Math.sqrt(d))/(2*A);
                     System.out.println("Root:"+root1);
                 }
                 else
                 {
                     throw new Exception ("B^2-4*A*B is negative and roots are imaginary");
                 }
                 inputFails = false;
            }
            catch (Exception e) {
               System.out.println(e.getMessage());
               System.out.println("Try again!");
               System.out.println();
            }
         }   
    } // end of main method
}  // end of class Quadratic



import java.util.Scanner;
import java.util.InputMismatchException;

class Lab08_Q2 {
   public static int getIntegerFromUser(int index) {
 
      Scanner key = new Scanner(System.in);

      int value = 0;
      boolean inputFails = true;
     
      while(inputFails) {  // Ask again if input is not valid
         try {
            System.out.print("Enter integer "+index+" : ");
            value = key.nextInt();
            if(value < 0) {           
               throw new Exception("Value is negative, Try again!");
            }
            inputFails = false; // Set inputFails to false if input is valid
         }
         catch (InputMismatchException e) {
            System.out.println("Value is not an integer, Try again!");
            key.next();  // bad input is removed from buffer
         }
         catch (Exception e) {
            System.out.println(e.getMessage()); // Writes the exception message to screen
         }
      }
      return value; // return the valid input
   }

   public static void main(String[] args) {
      System.out.println("Welcome to Average Calculator");
      System.out.println("Please enter 5 positive integers:");
      int sum = 0;
      for(int i = 1; i<=5; i++) {
         int value = getIntegerFromUser(i);
         sum += value;
      }
      double average = sum/5.0;
      System.out.println("The average is : " + average);
      System.out.println("Bye!");
   }
}


http://erdoganakbiyik.blogspot.com/
https://www.youtube.com/channel/UCDsUnmBfVdEPkcC8FlzPKcg

No comments:

Post a Comment