Friday, November 2, 2018

Lab 6 (Week 7 25/10/2018)

Halo, Programming again ^^
Lab 6 is all about character and string.
Strings, which are widely used in Java programming, are a sequence of characters. In Java programming language, strings are treated as objects.
The Java platform provides the String class to create and manipulate strings.

For example,  the most direct way to create a string is to write:
String greeting = "Hello world!";
In this case, "Hello world!" is a string literal—a series of characters in your code that is enclosed in double quotes. Whenever it encounters a string literal in your code, the compiler creates a String object with its value—in this case, Hello world!.

There were some tutorials given by lecturer in enhancing our understanding of this topic .

1.Show the output of the following statements:
a. System.out.printf( “%6b”, ( 1 > 2 ) );
* The output shown is false since 1 is smaller than 2.

b. System.out.printf( “%6s\n”, “Java”);
*Java displayed in cmd.
c. System.out.printf(“Amount is %f %e”, 32.32, 32.32);
d. System.out.printf(“%8d%8.1f”, 1234, 5.6321);
e. System.out.printf(“%-8d%-8.1f”, 1234, 5.6321);


2. Write these statements in a Java class and describe the output. Explain your observation.

String s1 = “Welcome to Java”; String s2 = “Welcome to Java”; String s3 = new String(“Welcome to Java”);

System.out.println( s1 == s2); System.out.println( s1 == s3);
*s1==s2 showed true because s1 is same as s2 while s1==s3 showed false because s1 is not same as s3.


3.Write an application that uses String method compareTo to compare two strings input by the user. Output whether the first string is less than, equal to or greater than the second.


4. Write a program that displays a random lowercase letter using the Math.random() method.
*A formula is used to generate a random lowercase letter.


5. Write a program that receives a character and displays its ASCII code (an integer between 1 and 127).
*ASCII code is being displayed by inserting int ASCII =(int)character;


6. Some websites impose certain rules for passwords. Write a method that checks whether a string is a valid password. Suppose the password rules are as follows:  A password must have at least eight characters.  A password consists of only letters and digits.  A password must contain at least two digits. Write a program that prompts the user to enter a password and displays Valid Password if the rules are followed or Invalid Password otherwise.

*The question asked to create a password with certain requirement. It quite complicated. But with the help of friends and google, I am able to solve it. This just a way to solve but I trusted that there still got some other ways to solve this question. 


After lab6, I am able to get understand in more detail about character and string. I will try to search other exercise in further improve my understanding. "Practice makes perfect". Thank you.

LIM XUE TING
183615
SSK3100 (Group15)

Next blog: hennyabigail.blogspot.com (Henny Abigailwillyen Sinjus)

No comments:

Post a Comment

Lab 10 (Week 13 13/12/2018)

Heyyy, we had lab 10 which is our last lab. It is about two dimensional array. What is two dimensional array?? Let me explain it ! The e...