|
input a, b, c;
Det = b*b - 4*a*c; // Compute the determinant
if ( Det >= 0 )
{
print -b/(2a) + sqrt(Det)/(2a); // Real number solutions
print -b/(2a) - sqrt(Det)/(2a);
}
if ( Det < 0 )
{
print -b/(2a) "+" (sqrt(-Det)/(2a) + "i"); // Complex number solutions
print -b/(2a) "-" (sqrt(-Det)/(2a) + "i");
}
|
import java.util.Scanner;
public class Abc3
{
public static void main(String[] args)
{
double a, b, c, Det, re, im;
Scanner in = new Scanner(System.in); // Construct Scanner object
a = in.nextDouble(); // Read in next number into a
b = in.nextDouble(); // Read in next number into b
c = in.nextDouble(); // Read in next number into c
Det = b*b - 4*a*c;
if ( Det >= 0 )
{
System.out.println( (-b + Math.sqrt( Det ) ) / (2*a) );
System.out.println( (-b - Math.sqrt( Det ) ) / (2*a) );
}
if ( Det < 0 )
{
re = -b/(2*a); // Compute real part
im = Math.sqrt( -Det )/(2*a); // Compute imaginary part
System.out.println( re + "+" + im + "i" );
System.out.println( re + "-" + im + "i" );
}
}
}
|
How to run the program:
|
Example:
Enter a:1 Enter b:2 Enter c:5 -1.0+2.0i -1.0-2.0i |
Shortcoming:
|
|
|
if ( CONDITION )
ONE-statement
else
ONE-statement
|
Explanation:
|
Note:
|
|
Schematically:
|
|
|
import java.util.Scanner;
public class Abc4
{
public static void main(String[] args)
{
double a, b, c, Det, re, im;
Scanner in = new Scanner(System.in); // Construct Scanner object
a = in.nextDouble(); // Read in next number into a
b = in.nextDouble(); // Read in next number into b
c = in.nextDouble(); // Read in next number into c
Det = b*b - 4*a*c;
if ( Det >= 0 )
{
System.out.println( (-b + Math.sqrt( Det ) ) / (2*a) );
System.out.println( (-b - Math.sqrt( Det ) ) / (2*a) );
}
else
{
re = -b/(2*a); // Compute real part
im = Math.sqrt( -Det )/(2*a); // Compute imaginary part
System.out.println( re + "+" + im + "i" );
System.out.println( re + "-" + im + "i" );
}
}
}
|
How to run the program:
|
|
|
import java.util.Scanner;
public class Max01
{
public static void main(String[] args)
{
double a, b, max;
Scanner in = new Scanner(System.in); // Construct Scanner object
a = in.nextDouble(); // Read in next number into a
b = in.nextDouble(); // Read in next number into b
if ( a >= b )
max = a;
else
max = b;
System.out.println( "max value = " + max );
}
}
|
How to run the program:
|
|
|
import java.util.Scanner;
public class Max01
{
public static void main(String[] args)
{
double a, b, max;
Scanner in = new Scanner(System.in); // Construct Scanner object
a = in.nextDouble(); // Read in next number into a
b = in.nextDouble(); // Read in next number into b
c = in.nextDouble(); // Read in next number into c
if ( a >= b ) // Find max(a,b)
max = a;
else
max = b;
if ( c > max ) // Check c > max ?
max = c;
System.out.println( "max value = " + max );
}
}
|
How to run the program:
|
|
Year Leap year ? Reason
---------- --------------- =================
1904 Yes Divisible by 4
1900 No Divisible by 100
2000 Yes Divisible by 400
|
|
import java.util.Scanner;
public class LeapYear01
{
public static void main(String[] args)
{
int year;
boolean leap;
Scanner in = new Scanner(System.in); // Construct Scanner object
year = in.nextInt(); // Read in year
if ( year % 4 == 0 )
leap = true;
else
leap = false;
if ( year % 100 == 0 )
leap = false;
if ( year % 400 == 0 )
leap = true;
System.out.println("Year is leap year ? " + leap);
}
}
|
How to run the program:
|
Example:
if ( a >= b ) ; // Bogus ;
max = a;
else
max = b;
|
Compiler message:
Error01.java:18: 'else' without 'if' else ^ |
because the compiler "reads" the program as follows:
if ( a >= b ) // A correct if-statement
; // Note: the if-statement ended here !
max = a; // A correct assignment statement
else // else ? Where is the if ???
max = b;
|
Example:
if ( Det >= 0 )
System.out.println( (-b + Math.sqrt( Det ) ) / (2*a) );
System.out.println( (-b - Math.sqrt( Det ) ) / (2*a) );
else
re = -b/(2*a); // Compute real part
im = Math.sqrt( -Det )/(2*a); // Compute imaginary part
System.out.println( re + "+" + im + "i" );
System.out.println( re + "-" + im + "i" );
|
The Java compiler will report the error 'else' without 'if' because syntactically, the program is read as follows:
if ( Det >= 0 )
System.out.println( (-b + Math.sqrt( Det ) ) / (2*a) ); // If-statement
System.out.println( (-b - Math.sqrt( Det ) ) / (2*a) ); // Print
else // Else without if
re = -b/(2*a); // Compute real part
im = Math.sqrt( -Det )/(2*a); // Compute imaginary part
System.out.println( re + "+" + im + "i" );
System.out.println( re + "-" + im + "i" );
|
Example:
if ( a >= b )
max = a // Missing semicolon !!!
else
max = b;
|
Compiler message:
Error03.java:17: ';' expected
max = a
^
|
Reason:
|
Example:
if ( a >= b )
{
max = a;
}; // bogus semicolon !!!
else
{
max = b;
}
|
Compiler message:
Error04.java:20: 'else' without 'if'
else
^
|
Because syntactically, the program is read as follows:
if ( a >= b ) // An if-statement
{
max = a;
}
; // An empty statement !!!
else // Else without if...
{
max = b;
}
|
The best thing is to stick to one useful form: use block !!!
if ( ..... )
{
(leave empty first)
}
|
I fill in the statements in the then-part and else-part later.
So even when the if-part and/or else-part consist of 1 statement, I write them as blocks.