LAB 6
Choong Kar Yee
182630
1.
public class Q1 {public static void main(String[] args) {
System.out.printf( "%6b", ( 1 > 2 ) );
System.out.printf( "%6s\n", "Java");
System.out.printf("Amount is %f %e", 32.32, 32.32);
System.out.printf("%8d%8.1f", 1234, 5.6321);
System.out.printf("%-8d%-8.1f", 1234, 5.6321);
}
}
2.
public class Q2 {
public static void main(String[] args) {
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);
}
}
3.
import java.util.Scanner;
public class Q3 {
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
System.out.print("Enter 2 name,sister1:");
String sister1= input.nextLine();
System.out.print("sister2:");
String sister2= input.nextLine();
if (sister1.compareTo(sister2)<0)
{
System.out.println("sister1 is smaller than sister2");
}
else if (sister1.compareTo(sister2)>0)
{
System.out.println("sister1 is greater than sister2");
}
else
{
System.out.println("They are same age!");
}
}
}
4.
public class Q4 {
public static void main(String[] args) {
int number=(int)(Math.random()* 10 +'a');
System.out.println((char)(number));
}
}
5.
import java.util.Scanner;
public class Q5 {
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
System.out.print("Enter a character:");
char E =input.next().charAt(0);
int ASCII =(int)E;
System.out.println("The ASCII code for character" + E + "is" + ASCII);
}
}
6.
import java.util.Scanner;
public class Q61 {
public static final int PASSWORD_LENGTH = 8;
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
System.out.print("A password must have at least eight characters.\n" +
"A password consists of only letters and digits.\n" +
"A password must contain at least two digits.");
System.out.print("Enterpassword");
String password= input.nextLine();
if (isValid(password)) {
System.out.println("Valid Password");
} else {
System.out.println("Invalid Password");
}
}
public static boolean isValid(String password) {
if (password.length() < 8) {
return false;
} else {
char c;
int count = 1;
for (int i = 0; i < password.length() - 1; i++) {
c = password.charAt(i);
if (!Character.isLetterOrDigit(c)) {
return false;
} else if (Character.isDigit(c)) {
count++;
if (count < 2) {
return false;
}
}
}
}
return true;
}
No comments:
Post a Comment