Presentation is loading. Please wait.

Presentation is loading. Please wait.

L AB #3. System.out.println(“if you have” +eggPerBasket + “egg per basket and” + numberOfBaskets +”baskets, then the total number off eggs is“+totalEggs);

Similar presentations


Presentation on theme: "L AB #3. System.out.println(“if you have” +eggPerBasket + “egg per basket and” + numberOfBaskets +”baskets, then the total number off eggs is“+totalEggs);"— Presentation transcript:

1 L AB #3

2

3 System.out.println(“if you have” +eggPerBasket + “egg per basket and” + numberOfBaskets +”baskets, then the total number off eggs is“+totalEggs);

4 -P ROGRAMMING E RRORS

5 Syntax Errors Detected by the compiler Runtime Errors Causes the program to abort Logic Errors Produces incorrect result P ROGRAMMING E RRORS

6 public class ShowSyntaxErrors { public static void main(String[] args) { i = 30; System.out.println(i + 4); } S YNTAX E RRORS

7 public class ShowRuntimeErrors { public static void main(String[] args) { int i = 1 / 0; } R UNTIME E RRORS

8 L OGIC E RRORS public class ShowLogicErrors { // Determine if a number is between 1 and 100 public static void main(String[] args) { // Prompt the user to enter a number String input = JOptionPane.showInputDialog(null, "Please enter an integer:", "ShowLogicErrors", JOptionPane.QUESTION_MESSAGE); int number = Integer.parseInt(input); // Display the result System.out.println("The number is between 1 and 100, " + "inclusively? " + ((1 <= number) && (100 <=number ))); System.exit(0); }

9 -C REATING AND E DITING U SING N OTE P AD -C OMPILING AND R UNNING J AVA FROM THE C OMMAND W INDOW

10 10 C REATING, C OMPILING, AND R UNNING P ROGRAMS

11 To use NotePad, type notepad Welcome.java from the DOS prompt(from all programs -> Accessories). 11 C REATING AND E DITING U SING N OTE P AD Open NotePad, write your code, save it as Welcome.java directly on c.

12 Set path to JDK bin directory set path=c:\ProgramFiles\java\jdk1.6.0\bin Set classpath to include the current directory classpath=c:\ProgramFiles\java\jdk1.6.0\bin 12 C OMPILING AND R UNNING J AVA FROM THE C OMMAND W INDOW

13 Open Dos then: Compile javac Welcome.java Run java Welcome

14 R EADING STATEMENT

15 1-R EADING STATEMENT 1. Write a program to read a name and then display the name with the existing greeting. The output should be "Welcome to Computer Programming Name!" where name is the value entered by the user. To read input from the command line, a Scanner object must he used. To use a Scanner object, the Java package java.util must be imported. Add the following line to the beginning of PersonalHello.java: import java.util.*;

16 public class PersonalHello { public static void main(String[] args) { String name; Scanner keyboard = new Scanner(System.in); System.out.print("What is your name?"); name = keyboard.next( ); System.out.println(" Welcome to Computer Programming! "); System.out.println(name + "!"); } Enter your first name only. Run code again then enter your full name with spaces. What happen?? Now: nextLine( ) Try nextLine( ) method instadeof next( ) compile and run again with your full name. What happen??

17 Exercise (1): Now try this code: public class PersonalHello { public static void main(String[] args) { String nameF,nameL; Scanner keyboard = new Scanner(System.in); System.out.print("What is your first name?"); nameF = keyboard.next( ); System.out.print("What is your last name?"); nameL = keyboard.next( ); System.out.println(" Welcome to Computer Programming "); System.out.println(nameF+” “+nameL + "!"); } try this: 1- type your first name then press enter, then type your last name 2- type your first name then space then type your last name What is the deference ?

18 E XERCISE (2): 1. Write a program to read a weight and height from user then calculate his body mass (bodyMass=weight/height 2 ). The output should be “your bodymass= bodyMass",where bodyMass is the result of the equation. nextDouble( );

19 public class PersonalHello { public static void main(String[] args) { double wieght, height; Scanner keyboard = new Scanner(System.in); System.out.print("What is your weight?"); weight = keyboard.nextDouble(); System.out.print("What is your height?"); height = keyboard.nextDouble(); System.out.println("your bodymass="+ (weight/(height*height))); } 3. Compile and run your modified program using the steps outlined above in the section entitled "Compiling and Executing a Program." 4. Show your lab instructor or assistant your working program.

20 D EBUGGING

21 Debugging Step-1 Activate the Line Numbers Go to Windows Preferences General Editor TextEditor Show line Numbers Prepared by Uzma Hashmi

22 Step-2 Setting Breakpoints To set breakpoints right click in the small left column in your source editor and select toggle break Or, you can double click on the particular place It indicates that we enter the break point It tells the debugger to stop at this point in the code When we do ‘RunAs’ it remains ineffective But when we run ‘DebugAs’ it tells the debugger to stop at the first point and suspends the program before this point Prepared by Uzma Hashmi

23 Step-3 Start Debugger You can debug your application by selecting your file, which contains a main method Right click it DebugAs Or, Select Run DebugAs Prepared by Uzma Hashmi

24 Debug Perspective Prepared by Uzma Hashmi

25

26 Terminate Ctrl+F2 Resume F8 Step Into F5 F6 Step Over F7 Step Return Prepared by Uzma Hashmi

27 The Current Stack is displayed in the debug view Stack In the debug mode it keeps the track of how many times debugger use on different intervals In the stack frame with pause symbol, shows where we are in the program in the main body. Initially Stack frame tells where we applied break Prepared by Uzma Hashmi

28 The editor highlights the first executable line. Prepared by Uzma Hashmi

29 The outline option on the RHS of the screen in debug mode tells us about the methods in our file and also highlights the method which is active in this case it is main Prepared by Uzma Hashmi

30 Variables and Breakpoints Views Variables listed with the value and the names and Breakpoints list where the breaks are applied Prepared by Uzma Hashmi

31 Variables values could be changed to see the effect in execution Changing Variable values Prepared by Uzma Hashmi


Download ppt "L AB #3. System.out.println(“if you have” +eggPerBasket + “egg per basket and” + numberOfBaskets +”baskets, then the total number off eggs is“+totalEggs);"

Similar presentations


Ads by Google