Download presentation
Presentation is loading. Please wait.
Published byAugustine Nelson Modified over 9 years ago
1
Computer Programming1 Computer Science 1 Computer Programming
2
2 Variables Variables are definred as follows Variable typeVariable nameValue int x= 5; this is saying “x holds a value of 5” doubley= 2.4; y String subject= “prog”; subject x 5 2.4 prog
3
Computer Programming3 Printing variables System.out.print(x); This prints the value of x, i.e. 5 System.out.print(y); This prints the value of y, i.e. 2.4 System.out.print(subject); This prints the value of subject, i.e. prog
4
Computer Programming4 We can also declare a variable and initialize it later int x; x =5; Declaring a variable and assigning a value is a statement. Remember all statements end with ;
5
Computer Programming5 If statements if statements take the following format: if(condition){ statements }// end if statement Note that the body of the if statement is indented
6
Computer Programming6 if Example public class ifTest{ public static void main(String args[]){ int age = 19; if (age >=18){ System.out.print(“You can vote”); }// end if statement System.exit(0); }
7
Computer Programming7 if else int age = 19; if(age >=18) Sytem.out.print(“You can vote”); else Sytem.out.print(“You cannot vote”);
8
Computer Programming8 Braces are needed around the statements in the if when there is more than one line, e.g. if(age >=18){ System.out.println("You can vote"); System.out.print(“Congratulations"); } else{ System.out.println("You cannot vote"); System.out.print(“Sorry!"); }
9
Computer Programming9 if else if int grade = 55; if (grade >= 80) System.out.print(“Distinction”); else if (grade >= 50) System.out.print(“Pass”); else System.out.print(“Fail”);
10
Computer Programming10 Inputting data We will look at two ways of Inputting data: 1: JOptionPanes 2: From the DOS prompt
11
Computer Programming11 JOptionPanes We first need to import the JOptionPane library at the very top of the file: import javax.swing.JOptionPane; Taking an input: Declare a string variable String name; name = JOptionPane.showInputDialog(“Enter name”); System.out.print(name); //prints name to DOS screen
12
Computer Programming12 JOptionPanes and Numbers When using JOptionPanes to take a number we need to variables, a String and an int String input; int number; This is because everything we enter into a JOptionPane is a string. Therefore if we enter a number (e.g. 5) we need to change it from a string value (“5”) to a number value (5).
13
Computer Programming13 import javax.swing.JOptionPane; public class Number{ public static void main(String args[]){ String input; int number; input = JOptionPane.showInputDialog(“Enter number”); //convert the string to an int number = Integer.parseInt(input); //print out number System.out.print(“Number is “ + number); System.exit(0); }//end main }//end class
14
Computer Programming14 Using a JOptionPane to display data JOptionPane.showMessageDialog(null,”The number is “ + number, “Results”,JOptionPane.INFORMATION_MESSAGE); First agruement is always null
15
Computer Programming15 Entering a number at the command prompt If we are taking a number from the user at the DOS screen we need to import the library: import java.util.Scanner; The only variable needed is an int. int number;
16
Computer Programming16 import java.util.Scanner; public class Age{ public static void main(String args[]){ Scanner input = new Scanner(System.in); int age; //prompt the user for an input System.out.print(“Enter your age”); age = input.nextInt(); System.out.print(“your age is “ + age); System.exit(0); }
17
Computer Programming17 For loops When we need to do something a number of times we can use a for loop. E.g. take in 3 numbers and add them up. Without a for loop the code would look like this: System.out.print(“Enter number 1”); number = input.nextInt(); total += number; System.out.print(“Enter number 2”); number = input.nextInt(); total += number; System.out.print(“Enter number 3”); number = input.nextInt(); total += number; System.out.print(“the total is “+total);
18
Computer Programming18 There is a lot of repetition in this code. However, if we were to use a for loop: int i, total = 0, number; String input; for(i = 1; i<=3 ;i++){ input = JOptionPane.showInputDialog(“enter number”); number = Integer.parseInt(input); total = total + number; }//end for System.out.print(“The total is “+ total);
19
Computer Programming19 Initialisation condition incrementation for (i=1 ; i <=3; i++){ statements } i starts with a value 1 The condition in the for loop is checked (is I (1) less than or equal to 3). If it is the statements are executed. After the statements are executed i is incremented(1 is added to i) This continues until the condition is no longer met, i.e. until I is no longer less than or equal to 3.
20
Computer Programming20 While loops A while loop takes the form: while (condition){ statements }
21
Computer Programming21 While loop examples 1)print values from 1 to 10 i= 1;//initialize i while ( i <=10){ System.out.println(i); i++;// increment i }// end while
22
Computer Programming22 Take in 5 grades using a while loop import javax.swing.JOptionPane; class grades{ public static void main(String args[]){ String input; int total =0, average, grade, i= 0; //start while loop to take grades while (i<5){ //take in an input input = JOptionPane.showInputDialog("Enter a grade"); //convert input to an int grade = Integer.parseInt(input); //add grade to total total += grade; //increment i i++; }//end while //get average average = total/5; //display results JOptionPane.showMessageDialog(null, "The total of the grades is "+ total + "\nThe average is "+ average ); }//end main }//end class
23
Computer Programming23 E.g. 2 While a non-digit is entered into a JOptionPane, give the user an error message and get the user to re-enter the input. //take an input from the user input = JOptionPane.showInputDialog(“Enter Number”); //give an error message and take the input again if non-digits entered while(!input.matches(“\\d+”)){ //show an error message JOptionPane.showMessageDialog(null, “Numbers only”); //take in input again input = JOptionPane.showInputDialog(“Enter Number”); }//end while //convert input to an int Number = Integer.parseInt(input);
24
Computer Programming24 While loop using a sentinel //take unknown amount of grades form a user //and get total of grades and average import javax.swing.JOptionPane; class sentinelTest { public static void main (String args[]){ String input; int grade, total = 0, counter =0; //take input from user input = JOptionPane.showInputDialog("enter a grade, -1 to quit"); //convert input to an int grade = Integer.parseInt(input); //loop to add to total and get another grade until -1 is entered while (grade != -1){ counter++; total += grade; input = JOptionPane.showInputDialog("enter a grade, -1 to quit"); //convert input to an int grade = Integer.parseInt(input); }//end while //check if -1 was entered first if (counter == 0){ JOptionPane.showMessageDialog(null,"No grades entered"); System.exit(0); } //if grades where entered display the results else { JOptionPane.showMessageDialog(null, "The total of the grades is "+ total + "\nThe average is "+ (total/counter) ); } }//end main method }//end class
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.