1 / 30

Chapter - 5

Chapter - 5. General Programming and Decision Making in Java. Program : It is a set of instructions given to a computer to perform a specific task. Package: It is a collection of classes to perform various functions.

kennan
Download Presentation

Chapter - 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter - 5 General Programming and Decision Making in Java

  2. Program : • It is a set of instructions given to a computer to perform a specific task. • Package: • It is a collection of classes to perform various functions. • Every package has a name and it can be included within a program by using the key word import. • Mathematical functions are automatically imported by the system to perform the desired task. • Math.sqrt() • This function is used to find the square root of a positive number. • Syntax: • <Return data type><variable> = Function name (Positive number) • Example: • double n = Math.sqrt(6.25); • It returns a double type value of n as 2.5

  3. Math.min() • This unction returns the minimum of two numbers. • The return value depends on the values used as the arguments of the function. • Syntax: • <Return Data type><Variable> = Function name ( argument 1, argument 2); • Example: • int n = Math.min(4, 6); • double n = Math.min(4.6, 2.8); • double x = Math.min(-3.5, -9.5);

  4. Math.pow() • This function is used to find the power raised to a specified base (number). • It always returns a double type value. • Syntax: • <Return Data type><variable> = Function name ( number 1, number 2); • Example: • double d = Math.pow(2.0, 3.0); • This function returns a double type value to d as 2.03.0 = 8.0 • Math.log() • Math.abs() • Math.round() • Math.floor() • Math.ceil()

  5. public class My_Maths { Public static void main ( Strings args []) { double m = 6.25; double n = 8.75; System.out.println(“ The output of the different mathematical functions are:”); System.out.println(“Square root of 6.25 & 8.75 = “+Math.sqrt(m)); System.out.println(“Minimum value between 6.25 & 8.75 = “+Math.min(m,n)); System.out.println(“6.25 raised to the power 3 = “+Math.pow(m,3)); } } public class My_Maths { Public static void main ( Strings args []) { System.out.println(“ The output of the different mathematical functions are:”); double n = Math.sqrt(6.25); System.out.println(“ The square root of 6.25 is :”+n); } }

  6. General Programming in Java • By assigning the values • By using BlueJ system • By using Input Stream. • By using scanner class. • Bu using command line argument. • Java programming by using BlueJ system. • This is one of the method to accept the values from the user at the time of execution of the program. • The data values and the variables are declared within the system. • Example: public static void main ( int p, int c, int b)

  7. public class Average { public static void main ( int p, int c, int b) { int s; float avg; s=(p+c+b); avg = s/3; System.out.println(“The total marks obtained = “ +s); System.out.println(“The average marks = “ +avg); } } Q. A computer manufacturing company announces a special offer to their customers on purchasing laptops and the printers accordingly. On laptop: Discount – 15 % On Printers : Discount – 10 % Write a program in Java to calculate the discount, if a customer purchases a laptop and a printer.

  8. Input by Using Stream Reader: • Package: • It is a collection of classes to perform various functions. • Every package has a name and it can be included within a program by using the key word import. • To develop a program using stream, some of the functions related to input output operations are invoked. • Buffer: • CPU or processor is the fastest device in a computer. Other peripheral devices are comparatively slower then processor . • Due to the speed differences it becomes difficult to have data communication between processor and peripheral devices. • A high speed memory is applied between I/O devices and processor used a s a bridge to synchronize their speeds. • The high speed temporary storage is termed as a Buffer.

  9. How to activate the Buffer? • import java.io.*; • Declaring a Java library package at the beginning of the program. • It allows performing all types of Input and Output task during the execution of the program. • public static void main(Strings args[]) throws IOException. • The statement “throws IOException” eliminates I/O errors in the program (if any) • It passes the report on I/O errors to the exception handler of Java system. • Declaring buffer to store the data values at the time of execution along with the IOException with the main() • InputStreamReader read = new InputStreamReader(System.in); • BufferedReader in = new BufferedReader(read);

  10. Syntax to accept the numeric data values: int n; System.out.println(“Enter a number”); n=Integer.parseInt(in.readLine()); Syntax to accept a character: char ch; System.out.println(“Enter a character”); ch = (char)(in.read()); Syntax to accept a string : String str; System.out.println(“Enter a String”); str = in.readLine();

  11. Q. Write a class with name employee and basic as its data mambers, to find the gross pay of an employee for the following allowances and deduction. Use meaningful variables. Dearness allowance = 25 % of Basic pay. House rent Allowance = 15 % of basic pay. Provident fund = 8.33 % of Basic pay. Net Pay = Basic Pay + dearness allowance + House rent. Gross pay = Net pay – Provident Fund.

  12. import java.io.*; public class Employee { Public static void main(String args[]) throws IOException { int basic; double da, hra, pf, npay, gross; gross=0; InputStreamReader read = new InputStreamReader(System.in); BufferedReader in = new BufferedReader(read); System.out.println(“Enter the Basic Salary”); basic=Integer.parseInt(in.readLine()); da=basic*25/100; hra=basic*15/100; pf=basic*8.33/100; npay=basic+da+hra; gross=npay-pf; System.out.println(“The net pay = “+npay); System.out.println(“The gross pay=“+gross); } }

  13. Input by using Scanner class. Scanner Class allows the user to read the values of various types. The scanner class is defined within a Java Package ( java.util.*). Or import java.util.scanner; Methods to read tokens from the scanner object. nextInt(): It receives the next token from the scanner object which can be expressed as an integer and stored in Integer type variable. Syantax : int <variable> = <ScannerObject> . nextInt( ); Example: Scanner in = new Scanner(System.in); int p; P = in.nextInt(); It means the variable p stores as an Integer through the object.

  14. import java.io.*; public class Employee { Public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.println(“Enter name of the employee and basic salary”); double da, hra, pf, npay, gross; String st; st=in.nextLine(); basic=in.nextLine(); da=basic*25/100; hra=basic*15/100; pf=basic*8.33/100; npay=basic+da+hra; gross=npay-pf; System.out.println(“Name of the Employee=“+st); System.out.println(“The net pay = “+npay); System.out.println(“The gross pay=“+gross); } }

  15. Input through Command Line Argument • It is a way to accept the data values (integer/character/Strings) from the user and sends the arguments to the main( known as command line arguments). • It does not require java package to perform input/output task. • While accepting the values from the console, the system stores the data values in different locations as array of strings. • The size of an array can be determined by using the built in functions args.length. • The arguments to the main() are passed through args[0], args[1], args[2] ……….. • p=

  16. // To find the value of the given expression using command line argument. Public class Expression { public static void main(Strings args[]) { int a, b; double p; //The value of a and b are entered through argument in String form a=Integer.parseInt(args[0]); b=Integer.parseInt(args[1]); p=Math.pow((a+b),3)/(a-b); System.out.println(“The value of the expression = “+p); } }

  17. Decision Making in JAVA The flow of control from one statement to other sequentially is termed as flow of control or sequential flow of control. The process of control flow takes place by default. Bi – Directional Branching The statement containing ‘if’ is referred as bi-directional branching. It check the given condition and transfers the control to either of two blocks of statements. If – Statement If statement used to check a specified condition. It performs a course of action if the condition is true otherwise, the action is ignored.

  18. Syntax: if(condition) { Statement_1; Statement_2; } Example: public class Program_1 { public static void main (String args[]) { int marks = 68; if(marks > 60) System.out.println(“First Class”); } }

  19. if – else statement It is a two way selector statement. The condition to be tested is evaluated and if it results is true value, then one of the two statements following the if will be executed. Otherwise, the statement following the else part is executed. Syntax: if (condition) statement_1; else statement_2; Example: if (x>=0) System.out.println(“X is a positive number”); else System.out.println(“X is a negative number”); Q. Write a program to accept a number and find whether it is an odd or even by using input statement.

  20. Import java.io.*; public class Program_2 { public static void main(String args[])throws IOException { int a; InputStreamReader read = new InputStreamReader(System.in); BufferedReader in = new BufferedReader(read); System.out.println(“Enter the number”); a=Integer.parseInt(in.readLine()); if (a%2 == 0) System.out.println(“The number is Even”); else System.out.println(“The number is Odd”); } }

  21. Nested if – statement If there are more than two alternative statements to be executed, then if-else does not work . Then we have to nest one or more if – else statement within the other if-else statement. Such a combination of if-else statement is referred to as nested if-statement. Q. Write a program to accept the three numbers and determines the largest among them using input statement. Q. Write a program to accept the marks of a student and declares the grade as per the following grade conditions Condition Grade >=80 A >=70 B >=60 C >=50 D >=40 E <40 F

  22. Import java.io.*; public class Program_3 { public static void main(String args[])throws IOException { int marks; Char grade; InputStreamReader read = new InputStreamReader(System.in); BufferedReader in = new BufferedReader(read); System.out.println(“Enter the percentage of marks”); marks=Integer.parseInt(in.readLine()); if(marks >= 80) grade = ‘A’; else if (marks >= 70) grade = ‘B’; else if (marks >= 60) grade = ‘C’; else if (marks >= 50) grade = ‘D’; else if (marks >= 40) grade = ‘E’; else if( marks < 40) grade = ‘F’;

  23. System.out.println(“The grade is : “+grade); } } Q. Accept a character from the keyboard. Display whether the entered character is an alphabet or a digit. If it is n alphabet, it should display whether it is a vowel or a consonant.

  24. Import java.io.*; public class Program_3 { public static void main(String args[])throws IOException { char a; InputStreamReader read = new InputStreamReader(System.in); BufferedReader in = new BufferedReader(read); System.out.println(“Enter the percentage of marks”); a=(char)(in.read()); if(a==‘A’ || a==‘a’ || a==‘E’ || a==‘e’ || a==‘I’ || a==‘i’ || a==‘o’ || a==‘O’ || a==‘u’ || a==‘U’); System.out.println(“Entered char is a vowel”); else if (Character.isDigit(a)) System.out.println(“Entered character is a digit); else System.out.println(“Entered character is a consonents”); } }

  25. Q. Accept the distance to be travelled , age and gender of the person. Depending on the inputs from the user display the fares as mentioned. Distance Fare of age above 60 Fare of Children Of travel M F below 10 Less than 100 150 100 50 Less than 300 500 400 250 Above 300 750 600 350

  26. Import java.io.*; public class Program_3 { public static void main(String args[])throws IOException { InputStreamReader read = new InputStreamReader(System.in); BufferedReader in = new BufferedReader(read); System.out.println(“Enter the approximate distance of the journey:”); int n = Integer.parseInt(in.readLine()); System.out.println(“Enter the age of the person”); int g = Integer.parseInt(in.readLine()); System.out.println(“Enter the gender of the person”); char c = (char)(in.read(); if (age <= 10) { If ( distance <= 100) System.out.println(“Fare is Rs. 50”); if (distance > 100 && distance <= 300) System.out.println(“Fare is Rs.250”); if ( distance > 300 ) System.out.println(“ Fare is Rs 350”);

  27. } if (age >= 60) { if(g==‘F’ || g== ‘f’) System.out.println( “Fare is Rs.100”); if(distance > 100 && distance <= 300) System.out.println(“ fare is rs.400”); if(distance > 300) System.out.println(“Fare is Rs.600”); } if(g==‘M’ || g==‘m’) { if(distance <= 100) System.out.println(“Fare is Rs.150”); if(distance > 100 && distance <= 300) System.out.println(“Fare is Rs.500.00”); If(distance > 300) System.out.println(“Fare is Rs.750”); } } If(age > 10 && age < 60) System.out.println(“Fare not defined”); }

  28. Q. Accept a number from the keyboard and depending on the user’s choice perform the following tasks: • Square root • Absolute value • Logarithm • Random number between 0 and 1.

  29. Import java.io.*; public class Program_3 { public static void main(String args[])throws IOException { InputStreamReader read = new InputStreamReader(System.in); BufferedReader in = new BufferedReader(read); System.out.println(“\t Menu”); System.out.println(“\t ======“); System.out.println(“1. Square Root\n 2. Absolute Value\n 3. Natural Logarithm\n 4. Random number between 0 and 1 “); System.out.println(“Enter your choice”); int n = Integer.parseInt(in.readLine()); switch (n) { case 1: System.out.println(“Enter the number to find the square root: “); int x = Integer.parseInt(num); System.out.println(“The square root of the number is:” +Math.sqrt(n)); break;

  30. case 2: System.out.println(“Enter the number to find the absolute value: “); int n1 = Integer.parseInt(num); System.out.println(“The absolute value of the number is: “ +Math.abs(n1); break; case 3: System.out.println(“Enter the number to find the logarithm”); int n2 = Integer.parseInt(num); System.out.println(“The logarithm of the number is:” +Math.log(n2); break; case 4: System.out.println(“The random number”+Math.random()); System.out.println() break; Default: System.out.println(“Invalid choice”); } } }

More Related