Presentation is loading. Please wait.

Presentation is loading. Please wait.

OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS

Similar presentations


Presentation on theme: "OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS"— Presentation transcript:

1 OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS
CS 201 OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS Copyright: Illinois Institute of Technology- George Koutsogiannakis

2 PREVIOUS TOPICS Using the Scanner class to read user input from the keyboard. Wrapper classes. Autoboxing / Unboxing.

3 NEW TOPICS Static Variables and their usage.
Using Command Line for user input. Using the JOptionPane for user input.

4 The Keyword Static The keyword static can be applied to methods and also instance variables of a class. Applied to methods: Methods that have been declared static can be called by using the name of the class they belong to. Therefore we do not need to instantiate an object of that class in order to invoke its static methods. An example is the methods of the library class Math. If we look at the API all the methods of this class have the word static in their signature. That means that we can call method pow, as an example, by : double a= Math.pow(a, b);

5 Keyword static applied to methods
We have also seen static used in classes where there is a main method in addition to other methods. Rule: If we want to invoke, from the main method, another method of the same class (that the main method belongs to) then that method has to be declared static. There is another way to call a non static method from the main (assuming that both are in the same class) by creating an object of the same class as the main and invoking the method by using the object.

6 Static applied to methods
The previous rule applies to all methods which are static in a class and not just the main method!

7 Static applied to methods
We Know by now that the main method is a static method i.e. public static void main(String[] args) {…..} RULE: You can only call static methods from within a static method. Non static methods can be invoked from within a static method by using an object of the class that the static method belongs to.

8 Static applied to methods- Calling other methods from a static method
Example public class SomeClass { public static void main(String[] args) //some code here //next we want to make a call to the method AnotherMethod int x=AnotherMethod(); } public int AnotherMethod() int var=0; //some code sets the value of var return var THE COMPILER WILL GIVE US AN ERROR MESSAGE BECAUSE WE ARE TRYING TO CALL A NON STATIC METHOD FROM WITHIN A STATIC METHOD

9 Static applied to methods- Calling other methods from a static method
HOW DO WE FIX THE ERROR? TWO OPTIONS Declare the method AnotherMethod in our example to be static public static int AnotherMethod() { //code } The other option is to use an object of class SomeClass to invoke the method AnotherMethod The code for this choice is shown on the next slide.

10 Static applied to methods- Calling other methods from a static method
public class SomeClass { public static void main(String[] args) //some code here //next we want to make a call to the method AnotherMethod SomeClass sc = new SomeClass(); int x= sc. AnotherMethod(); } public int AnotherMethod() int var=0; //some code sets the value of var return var

11 Keyword Static applied to instance variables (attributes) of a class
If an instance variable (field) is declared static then its value can be seen by all objects of the same class. i.e static int id=0; id++; the ++ after the identifier id means that the its value is incremented by one every time we execute the line of code id++

12 Static Variables Thus if id=0; id++ causes a new value of id=1
calling id++ again causes a new value of id=2 And so on. If id is also declared static then every time we increment the value of id every object of the class sees the new value.

13 Static Variables Suppose that id is a static variable and that it belongs to class Student (template class). Suppose that in StudentClient we have instantiated the following objects: Student student1=new Student(); Student student2=new Student(); Student student3=new Student(); If the value of id=3 then all the student objects share the same value for id (that is 3).

14 Example of static variable
public class Student { static int id=0; int currentID=0; String studentName=" "; public Student() id++; currentID=id; studentName=“John Doe”; } public Student ( String sn) Studentname=sn; followed by accessor /mutator/toString() methods

15 Example of static variable
We notice that in class Student Every time the constructor is called the value of id is advanced by one with respect to its previous value. Once the value of id is advanced its new value is assigned to a non static variable : currentID. That is because the non static variables have values that are dedicated to each object and their values are not shared by all objects of the class.

16 Example of static variable
public class StudentClient { public static void main(String [] args) Student st1=new Student() // value of id=1 now for st1 . Every time we call the constructor of the class Student id advances by 1 //Also,The value of id is passed to currentID =1 Student st2=new Student() // value of id=2 now for both st1 and st2, // currentID=1 for st1 but // currentID=2 for st2. Student st3=new Student() // value of id=3 for st1, st2, st3 // currentID=1 for st1 // currentID=2 for st2 // currentID=3 for st3

17 Example of static variable
Therefore we could use the static variable id to track how many objects we have created for that class (i.e. the Student class). Notice that the new value of the static variable id is shared amongst all objects of the class.

18 Example of static variable
The value of id is the same for each object student3 is created: Student 3 Student 1 Student 2 id-=3 id-=3 id-=3 currentID=1 currentID=2 currentID=3 Since id is static all 3 objects see its new value when it changes (if a 4th Student was to be created all 4 objects will have id=4). The variable currentID is not static therfore each object sees the value of currentID assigned for that object alone.

19 Command Line Input We have used the scanner object to obtain input from a user via the keyboard. Another way of obtaining input for a program is from the DOS pane when we use the interpret command. This is called “command line “ input. i.e >java StudentClient arg1 arg2 arg3 Where arg1 arg2 arg 3 will be read as individual Strings by the main method of the program. This approach is only good as long as the class has a main method (can not be used to input data into a template class).

20 Command Line Input Suppose that we have the class StudentClient and that it expects 3 inputs (called arguments) from command line >java StudentClient arg1 arg 2 arg3 public class Student public static void main(String [] args) { String a=args[0]; String b=args[1]; String c=args[2];

21 Command Line Input The Strings a , b , c now have the values that the user entered: i.e. >java StudentClient Joe , 20, 4.65 thus: a has the value “Joe” b has the value “20 “as a String c has the value “4.65” as a String

22 Command Line Input If we wanted to do arithmetic operations with the String versions of values 20 and 4.65 then we have to parse the String verions by using the appropriate wrapper class i.e int x= Integer.parseInt(b); results in the int version of the String 20 (int x=20) double y=Double.parseDouble( c ); results in the double version of the String (double y=4.65)

23 JOptionPane class The JOptionPane is a pre defined class that has methods that allow the creation of a graphical message box. The user types into the box and the program captures what the user typed as a String data type. We can change the data type from a String to another type by using the proper Wrapper class and by parsing. We can respond back to the user by displaying another graphical box that has the output of the program.

24 JOptionPane static Methods
Return value Method name and argument list String showInputDialog( Component parent, Object prompt ) pops up an input dialog box, where prompt asks the user for input. void showMessageDialog( Component parent, Object message ) pops up an output dialog box with message displayed See Examples 3.19 DialogBoxDemo1.java

25 JOptionPane Notice that since the methods are static we can call them (use them) by invoking them with the name of the class they belong to and the dot operator i.e String str=JOptionPane.showInputDialog(null, “Information to User”, “Please enter a whole number”, JOptionPane.INFORMATION_MESSAGE)

26 Example of JOptionPane
import javax.swing.JOptionPane; public class TestJOptionPane { public static void main(String[] args) String str=JOptionPane.showInputDialog(null, "Input from User", "Enter a integer", JOptionPane.INFORMATION_MESSAGE); int x=Integer.parseInt(str); JOptionPane.showMessageDialog(null, "The value you entered was"+x); System.exit(0); }

27 Example of JOptionPane
Notice that the package javax.swing needs to be imported.

28 Study Guide Chapter 7 section 7.11 Chapter3 section 3.16


Download ppt "OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS"

Similar presentations


Ads by Google