Exercise 6. Learning objective: Make a decision based on 2 conditions both being
ID: 3920188 • Letter: E
Question
Exercise 6. Learning objective: Make a decision based on 2 conditions both being true using the &s; operator Class to edit: Exereises Write a user login program that asks the user to input a username and then a password. If the username equals joe and the password equals quees.your program should aooept this as the correct login and print the message·wei eone· jee!". Otherwise your program, should print the message . Incorrect username or password.. The following sample LiO shows how your program should behave when both the username and password are correct Usernanenpute joe Welcone Jel Otherwise, your program should bohave as folows Usernanesuser inputs joe user inputs seeretExplanation / Answer
import java.util.Scanner;
class Main {
private static final Scanner scanner = new Scanner(System.in);
public static String askString(String question){
System.out.print(question + " ");
return scanner.nextLine();
}
public static int askInt(String question){
System.out.print(question + " ");
int result = scanner.nextInt();
scanner.nextLine();
return result;
}
public static double askDouble(String question){
System.out.print(question + " ");
double result = scanner.nextDouble();
scanner.nextLine();
return result;
}
public static char askChar(String question){
System.out.print(question + " ");
return scanner.nextLine().charAt(0);
}
public static boolean askBoolean(String question){
System.out.print(question + " ");
boolean result = scanner.nextBoolean();
scanner.nextLine();
return result;
}
public static void main(String[] args) {
String uname = askString("Username:");
String pwd = askString("Password:");
if(uname.equals("joe") && pwd.equals("guess"))
System.out.println("Welcome, joe!");
else
System.out.println("Incorrect username or password.");
System.out.println();
double radius = askDouble("Enter circle radius:");
System.out.println("The area of the circle is " + (Math.PI*radius*radius));
System.out.println();
double x1 = askDouble("Enter x coordinate for point 1:");
double y1 = askDouble("Enter y coordinate for point 1:");
double x2 = askDouble("Enter x coordinate for point 2:");
double y2 = askDouble("Enter y coordinate for point 2:");
System.out.println("The distance between the two points is "+(Math.sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1) * 1.0)));
System.out.println();
double side = askDouble("Enter square side length:");
double area = side*side;
System.out.println("The area of the square is "+area);
System.out.println();
}
}
/*SAMPLE OUTPUT
Username: joe
Password: guess
Welcome, joe!
Enter circle radius: 2.5
The area of the circle is 19.634954084936208
Enter x coordinate for point 1: 2
Enter y coordinate for point 1: 1
Enter x coordinate for point 2: 5
Enter y coordinate for point 2: 5
The distance between the two points is 5.0
Enter square side length: 2.5
The area of the square is 6.25
*/
import java.util.Scanner;
class Main {
private static final Scanner scanner = new Scanner(System.in);
public static String askString(String question){
System.out.print(question + " ");
return scanner.nextLine();
}
public static int askInt(String question){
System.out.print(question + " ");
int result = scanner.nextInt();
scanner.nextLine();
return result;
}
public static double askDouble(String question){
System.out.print(question + " ");
double result = scanner.nextDouble();
scanner.nextLine();
return result;
}
public static char askChar(String question){
System.out.print(question + " ");
return scanner.nextLine().charAt(0);
}
public static boolean askBoolean(String question){
System.out.print(question + " ");
boolean result = scanner.nextBoolean();
scanner.nextLine();
return result;
}
public static void main(String[] args) {
String uname = askString("Username:");
String pwd = askString("Password:");
if(uname.equals("joe") && pwd.equals("guess"))
System.out.println("Welcome, joe!");
else
System.out.println("Incorrect username or password.");
System.out.println();
double radius = askDouble("Enter circle radius:");
System.out.println("The area of the circle is " + (Math.PI*radius*radius));
System.out.println();
double x1 = askDouble("Enter x coordinate for point 1:");
double y1 = askDouble("Enter y coordinate for point 1:");
double x2 = askDouble("Enter x coordinate for point 2:");
double y2 = askDouble("Enter y coordinate for point 2:");
System.out.println("The distance between the two points is "+(Math.sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1) * 1.0)));
System.out.println();
double side = askDouble("Enter square side length:");
double area = side*side;
System.out.println("The area of the square is "+area);
System.out.println();
}
}
/*SAMPLE OUTPUT
Username: joe
Password: guess
Welcome, joe!
Enter circle radius: 2.5
The area of the circle is 19.634954084936208
Enter x coordinate for point 1: 2
Enter y coordinate for point 1: 1
Enter x coordinate for point 2: 5
Enter y coordinate for point 2: 5
The distance between the two points is 5.0
Enter square side length: 2.5
The area of the square is 6.25
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.