Click here to Skip to main content
15,887,361 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello everybody

I need to write a Junit for a class that find a squre equation:

the code:

import java.util.Scanner;

public class SquareEquation {
	private static boolean ans = true;


	public static void main(String[] args) {
		sqEquation();
	}
	public static void sqEquation(){

		while (ans==true) {

			double a;
			double b;
			double c;

			System.out.println("aX^2+bX+c=0: Enter a,b,c:");
			System.out.print("Enter a:");
			Scanner s = new Scanner(System.in); 
			a = s.nextDouble(); 	
			System.out.print("Enter b:");
			Scanner y = new Scanner(System.in);
			b = y.nextDouble(); 	
			System.out.print("Enter c:");
			Scanner z = new Scanner(System.in); 
			c = z.nextDouble();	

			sqEq(a,b,c);
		}
		System.out.println("bye bye");
	}
	public static void sqEq(double a, double b, double c) {

		double x1 = (-b + Math.sqrt(b*b-4*a*c))/(2*a);
		double x2 = (-b - Math.sqrt(b*b-4*a*c))/(2*a);


		if((b*b-4*a*c)>0) {
			System.out.println("x1: "+ x1+"  "+"x2:" +x2);
			continuegame();		
		}

		else if((b*b-4*a*c)==0 && checkabc(a,b,c)) {
			System.out.println("x1=x2="+x1);
			System.out.println(a+"X^2"+"+"+b+"X"+"+"+c+"=0");
			continuegame();		
		}


		else {
			try {
				if((b*b-4*a*c)<0) {
					throw new SquareEquationException ("Error: NO real roots!");
				}
				if(a==0&&b==0&&c==0) {
					throw new SquareEquationException ("x can be any number - trivial");
				}
				if(a==0&&b==0&&c!=0) {
					throw new SquareEquationException ("Error, no answer!");
				}

			}
			catch (SquareEquationException e) {
				e.printStackTrace();
			}


			finally {
				continuegame();
			}

		}
	}

	public static boolean continuegame () {
		System.out.println("Enter 0 or any number to Exit or 1 to solve aX^2+bX+c=0");
		Scanner f = new Scanner(System.in); 
		int k;
		k = f.nextInt();
		if (k==1) {
			ans=true;
			return ans;
		}
		else {
			ans=false;
			return ans;
		}
	}

	public static boolean checkabc (double a, double b, double c) {
		if(a==0&&b==0&&c==0) {
			return false;
		}
		if(a==0&&b==0&&c!=0) {
			return false;
		}
		else {
			return true;
		}

	}

}





how can I write a Junit to this class? that check
1. the option that I have 2 solutions
2. the options that I have a solution
3. the option that I don't have any solution


I don't success to do it..

thank you!!

What I have tried:

I try to do Junit to this class and I don't success with assertequals and with syntax of junit for exception and I need some example that working////
Posted
Comments
[no name] 7-Apr-19 21:16pm    
You said you "tried" ... then show what you tried. There is no free lunch.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900