Dasar-Dasar Pemrograman : String, Input-Output I dalam Java
String
One of many data type that also commonly used in computer programming is string. String is a sequence of characters. In Java, there is a predefined class called string class Sring class used to store and process strings of characters. Strings of characters are written within double quotes.
Example
String motto = "Wombats are kind-hearted.";
String Concatenation
Concatenation is the way connecting strings to get a longer string. Concatenation operator (+) is used to concatenate two strings.
String Methods
- int length()
- String trim()
- boolean equals(String otherString)
- boolean equalsIgnoreCase(String otherString)
- String toLowerCase() & String toUpperCase()
- char charAt(int position)
- int indexOf(int character) & int indexOf(String otherString)
- String substring(int beginIndex)
- String substring(int beginIndex,int endIndex)
- int compareTo(String otherString)
Input
In computer software, input is the information needed for a program to complete its execution. For simple examples where input can come from :
- Graphical component like dialog box to accept username and password from a user
- Clicking a mouse in specific area in the screen
- Data from a network connection
- Other devices: scanner, digital camera, touchscreen
Output
Output is an information that the program must “deliver” to the user. For, examples where output can come out :
- Computer screen
- Printer
- Speaker
Screen Output
Java has some syntax that can be used to output an information to the screen:
- System.out.println(args)
- System.out.print(args)
args could be nothing, boolean, char, char[], double, float, int, long, Object, String. To show the output more than one item, place a plus (+) sign between the items
println is add a new line after the argument so the next output goes on a new line, while print is the next output is placed on the same line.
Formatted Output
Consider a statement to output a real value
Output : 3.142857142857143
The output can be formatted, so that the output will look like this : 3.14 or 3.14285
Java provides several approaches to format any numeric and other type of output :
- NumberFormat Class
- DecimalFormat Class
- printf() method from System.out
- format() method from System.out
System.out.printf
System.out.printf() is a convenience method to write a formatted string to the output.
Syntax :
- ’l’ : is locale (usually no localization is applied)
- ’format’ : a format string/specifier
- ’args’ : arguments referenced by the format specifiers in the format string.
Format Specifer
Example
Scanner
Java’s class for a simple keyboard input is scanner. Import that class to make it available to our program
Create the scanner object :
Keyword new is used to instantiate an object from a class. Then, sc is the name of variable that holds a Scanner object
Scanner’s Methods
How to Use Scanner’s Method
After you create a Scanner object, you can use the method using this syntax :
OBJECT_NAME.METHOD_NAME()
Exercises and Try to Code These !
Write a program to solve each problem below :
1.Womboy runs in constant speed for h hours. If his speed is s km/hours, calculate the distance that he travel in kms !
2.Excluding stoppages, the speed of a bus is a kmph and including stoppages, it is b kmph. Make a program to calculate for how many minutes does the bus stop per hour ?
3.Three families share a garden. They usually clean the garden together at the end of each week, but last week, family C was on holiday, so family A spent a hours, family B spent b hours and had everything done. After coming back, family C is willing to pay $d to the other two families. How much should family A get? You may assume both families were cleaning at the same speed. Write a program to solve this problem!
Tip : You can make the algorithm by using BlueJ or NetBeans for Java. Also you can try to program these with Pascal, C, C++, or Phyton.
Sumber
Slide Basic Programming : String and I/O in Java
Posting Komentar