4. Introduction to Java (Cont)
4. Introduction to Java (Cont)
Command input
Input Scanner
File Scanner
Packages in Java
Bag Box
20 20
Typically you’d create an object from a class, fills its fields with some values
and maybe create another object and fill its fields with different values
but then eventually both those object will get destroyed including every single
value stored in those fields
Item
String name
static double weight
Item
String name
static double weight 20
Bag Box
20 20
Item
String name
static double weight 10
Bag Box
E.g., access the weight field from the class Item directly and set it
to a value
It’s ideally used to create a method that doesn’t need to access any fields
in the object
A static method takes input argument and returns a result based only on
those input values and nothing else
that’s because static fields also belong to the class and are shared among all
objects of that class
Example
public class Calculator{
public static int add(int a, int b){return a+b;}
public static int substract(int a, int b){return a –b;}
}
Since both add and subtract don’t need any object-specific values,
they can be declared static as seen above
and hence you can call them directly using the class name
Calculator without the need to create an object variable at all
Calculator.add(3,3);
E.g., the Math class has a bunch of static methods like random()
If the containing object is destroyed, the composed objects usually are as well
class Person{
class Person{
….
public MyDate getBirthday(){
return birthday;
}
}
E.g., for a contact manager application, it has some useful methods, but to
use them we have to write all the code in the main method including all your
friends’ contact details
This way, users have to write code and recompile it every time they want to
make a change!
Java allow us to accept input from the user while the program is running
i.e., write the main method in a way that ask the user to input their friends’ names,
phone numbers… then pass that information on to be stored.
There are 4 different ways a java program can read input from the user
Command line arguments
Runtime input
Files
Graphical User Interface (wont be covered in this course)
A Scanner allows the program to read any data type from a particular input,
if we create the scanner object like this
If you want to read a number into an integer variable instead of the entire
line, then use the method nextInt()
To read a text file in java, you can also use the Scanner class,
but instead of reading the command line inputs by passing System.in as the
argument,
you pass a File object which you can create by typing in the file name
Then, you can read lines the same way we did before (use nextLine())
To check if the file still has more lines, you can use hasNextLine
method in case you want to load the entire file
java.lang contain classes for defining primitive data types & math operations
(this package imported automatically)
java.awt classes for implementing the components for GUI like buttons, menu…
package myPackage ;
public class MyClass{
public void getMessage(String s){
System.out.println(s);
}
}
Import myPackage.MyClass;
obj.getMessage(msg);
}
}