P. 1
Lab-Notes

Lab-Notes

Ratings: (0)|Views: 255|Likes:
Published by kussh

More info:

Published by: kussh on Jul 31, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

05/11/2014

pdf

text

original

 
OOPS Through JAVA
1. Program for printing the roots of Quadratic equation
 Description:
In this program we are finding the roots of the quadratic equation. Dependsupon the value of the determinant we are printing the nature as well as thecorresponding roots. The particular part is explained below:
 public static void main ( String args[] ) :
Every program begins with the main() method. All the Java applications begin execution by calling main(). The full meaning of each part of this is:
 public:
The
 public
keyword is an access specifier, which allows the programmer tocontrol the visibility of class members. When a class member is preceded by
 public,
then that member may be accessed by code outside the class in which it isdeclared.
main( )
must be declared as public, since it must be called by codeoutside of its classe when the program is started.
 static:
The keyword
 static
allows
main( )
to be called without having to instantiatea particular instance of the class. This is necessary since
main ( )
is called by theJVM before any objects are made.
void:
The keyword
void 
simply tells the compiler that
main ( )
doesn’t return avalue.
String args [ ]:
In
main ( ) ,
there is only one parameter, albeit a complicated one.
String args [ ]
declares a parameter named args, which is an array of instances of theclass String.
args
receives any command-line arguments present when the programis executed.
 Math . sqrt ( ):
1
 
OOPS Through JAVA
 sqrt ( )
is a member of the Math class, to compute the square root of itsargument. Its arguments are of type
double
.
 switch case:
 switch
is a multi-way decision making decision control statement. The
 switch
statement tests the value of a given variable or expression against a list of 
case
values and when a match is found, a block of statements associated with thatcase is executed. The general form of it is :
 switch
(
expression
){
case
value 1 : block 1
break;case
value 2 : block 2
break;
………
default 
: default block 
break;
}statement – x ;After the switch statements terminates statement – x will be executed. Theexpression must be of type byte,short,int or char each of the values specified in thecase statements must be of a type compatible with the expression. Each casevalue must be a unique literal. Duplicate case values are not allowed.
2
 
OOPS Through JAVA
 Program:
class Equation{int a , b , c , ch ;double e , d , r1 , r2 ;Equation ( int x , int y , int z ){a = x ;b = y ;c = z ;}public void value ( ){d = ( b * b ) - ( 4 * a * c ) ;e = Math.sqrt ( d ) ;if ( d == 0 )ch = 1 ;elseif ( d > 0 )ch = 2 ;elseif ( d < 0 )ch = 3 ;switch ( ch ){case 1:System.out.println ( " Roots areReal & Equal " ) ;r1 = -b / ( 2.0 * a ) ;r2 = r1 ;System.out.println ( " R1 : " + r1 +" R2 : " + r2 ) ;break;case 2:System.out.println ( " Roots areReals and Unequal " ) ;r1 = ( -b + ( e/ ( 2 * a ) ) ) ;r2 = ( -b - ( e/ ( 2 * a ) ) ) ;System.out.println ( " R1 : " + r1 +" R2 : " + r2 ) ;break;default:System.out.println ( " Roots AreImaginary " ) ;}}public static void main(String[] args)
3

Activity (14)

You've already reviewed this. Edit your review.
1 hundred reads
Sridhar Sree liked this
Sridhar Sree liked this
Tejaswi Atluri liked this
Tejaswi Atluri liked this
jocansino liked this
abdulnajib liked this
venkat141 liked this
yarjun_hawari500 liked this

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->