1. Write a program that asks the user to enter three names, and then displays the names sorted alphabetically in descending order. For example, if the user entered "Charlie", “Leslie", and "Andy", the program would display: Leslie Charlie Andy
Need help with step 4. Below are my work for steps 1 and 2 and 3 are the screenshot codes.
THIS IS MY CODE FOR STEP 1 -
import java.util.Scanner;//to readinput
public class Main
{
public static void main(String[] args) {
String s1,s2,s3;//to store 3 names
Scanner sc = new Scanner(System.in);//to read input
//reading 3 names from user
System.out.println("Enter three names :");
s1=sc.next();
s2=sc.next();
s3=sc.next();
System.out.println("Descending order:");
//printing 3 names, in descending order
if(s1.compareTo(s2)>=0 && s2.compareTo(s3)>=0)//if s1>=s2>=s3
{
System.out.println(s1+"\n"+s2+"\n"+s3);
}
else if(s1.compareTo(s3)>=0 && s3.compareTo(s2)>=0)//if s1>=s3>=s2
{
System.out.println(s1+"\n"+s3+"\n"+s2);
}
else if(s2.compareTo(s1)>=0 && s1.compareTo(s3)>=0)//if s2>=s1>=s3
{
System.out.println(s2+"\n"+s1+"\n"+s3);
}
else if(s2.compareTo(s3)>=0 && s3.compareTo(s1)>=0)//if s2>=s3>=s1
{
System.out.println(s2+"\n"+s3+"\n"+s1);
}
else if(s3.compareTo(s1)>=0 && s1.compareTo(s2)>=0)//if s3>=s1>=s2
{
System.out.println(s3+"\n"+s1+"\n"+s2);
}
else if(s3.compareTo(s2)>=0 && s2.compareTo(s1)>=0)//if s3>=s2>=s1
{
System.out.println(s3+"\n"+s2+"\n"+s1);
}
}
}
THIS IS MY CODE FOR STEP 2 -
public static boolean isLeapYear(int year) { return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); }
public static void main(String[] args) { Scanner keyboard = new Scanner(System.in);
int year; do {
System.out.print("Enter an year(0 to exit): ");
year = keyboard.nextInt();
if (year > 0) {
if (isLeapYear(year)) {
System.out.println(year + " is a leap year");
} else {
System.out.println(year + " is not a leap year"); }
}
} while (year != 0);
}
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images