Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.

Similar presentations


Presentation on theme: "Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation."— Presentation transcript:

1 Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh Bajaj, 2006. All rights reserved

2 Objectives Understand programming constructs in Java: -the block -the if statement -the switch statement Let’s get started!

3 Block Any number of Java statements, surrounded by a pair of braces Blocks define the scope of variables. We should not have identically named variables in 2 different blocks in the same method The scope of a variable starts at the place it was declared, and goes to the end of that block. If we want to have a variable accessible to all the code in a method like main, we must declare it outside of any of the blocks in main, i.e., within the braces that enclose all the main code, but outside any of the sub blocks in main. TestBlocks.java

4 Block Example: { System.out.print("The answer is "); System.out.println(ans); } { // This block exchanges the values of x and y int temp; // A temp variable for use in this block. temp = x; // Save a copy of the value of x in temp. x = y; // Copy the value of y into x. y = temp; // Copy the value of temp into y. } Part of one program with two blocks is shown above. Note how the temp variable is only declared within the second block and only used there. The variable cannot be used outside of this block, even in the same method. Usually, we write methods that are small enough that we declare all variables at the start of the method.

5 If statement Syntax: if(boolean expression) { /* some code below */ ---------; ----------; } else { /* some code below */ ---------; ----------; }

6 If-else-if statement Syntax: if(boolean expression1) { /* some code below */ ---------; ----------; } else if(boolean expression2) { /* some code below */ ---------; ----------; } else if(boolean expression3) { /* some code below */ ---------; ----------; } else { /* some code below */ ---------; ----------; } What happens if boolean expression 1 is true? If 2 is true? If 3 is true? If none are true? What if 1 & 2 are true? What if 2 and 3 are true? What if all are true?

7 Switch statement Syntax: switch (expression) { case constant-1: //statements break; case constant-2: //statements- break; // (more cases). case constant-N: //statements break; default: // optional default //statements } // end of switch statement Expression must be int or char The break is optional. What happens if we don’t put the break? What happens if we don’t put the default?

8 Switch statement switch (N) { // assume N is an integer variable case 1: System.out.println("The number is 1."); break; case 2: case 4: case 8: System.out.println("The number is 2, 4, or 8."); System.out.println("(That's a power of 2!)"); break; case 3: case 6: case 9: System.out.println("The number is 3, 6, or 9."); System.out.println("(That's a multiple of 3!)"); break; case 5: System.out.println("The number is 5."); break; default: System.out.println("The number is 7,"); System.out.println(" or is outside the range 1 to 9."); } What happens if N = 12? What happens if N = 6?

9 Switch versus If Usually, we should use if without else An if-else if-else if-else –if statement will usually work better than a switch statement In the switch statement, the expression must be int or char In the if-else-if statement, we need a boolean expression In some cases, like menu processing, we can use the switch statement http://nfp.cba.utulsa.edu/bajaja/mis3023/Text/javanotes4.1/c3/s6.html Shows an example of using a menu selection

10 Fun In Class Assignment Write a java program, called ComputeTotalSalary.java for our sales force. The program should use the TextIO API. It should ask the user to input the name of the salesperson (String), the base salary and the total sales of last year (both doubles). If the sales are below zero, the program should print an error and exit. If the sales are between $1 and $25,000, the program should add a bonus of 5% of the sales to the base. If the sales are between $25,000 and $50,000, the program should add a bonus of 10% of sales. If the sales are between $50,000 and $100,000 it should add a bonus of 15% of sales. Sales over $100,000 merit a 20% -of-sales bonus. Finally, the program should print the statement: The final salary of ----- is $XXXX.XX and exit.


Download ppt "Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation."

Similar presentations


Ads by Google