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 Note that these three names are just an example. The names could be any words. 2. Write a program that asks the user to enter a year, and then displays whether that year is a leap year or not. Note that a year is a leap year if the year is a multiple of 400, or it is a multiple of 4 and not a multiple of 100. 3. Write a program to repeatedly read integers from the standard input until it reads -1 and exits. For each number before -1, if it is positive and odd, do the following: • Output a triangle like the one below with the integer being the width of the triangle's bottom or the number of asterisks of the bottom row. For instance, if the integer is 7, you would display the triangle below: Or if the integer is not positive or not odd, do nothing and continue to read another integer. 4. A segment of an array is defined as a collection of consecutive elements in that array. The length of a segment is the number of elements in the segment. A repeating segment is a segment with all the elements in it having the same value. The following method returns the length of the longest repeating segment in a given int array. Complete the implementation of the method. public int getLength0fLongestRepeatingSegment(int [0 array) { // add your implementation here 3
Need help with step 3. Below are my work for steps 1 and 2.
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);
}
}
![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
Note that these three names are just an example. The names could be any words.
2. Write a program that asks the user to enter a year, and then displays whether that year is a leap
year or not. Note that a year is a leap year if the year is a multiple of 400, or it is a multiple of
4 and not a multiple of 100.
3. Write a program to repeatedly read integers from the standard input until it reads -1 and exits.
For each number before -1, if it is positive and odd, do the following:
• Output a triangle like the one below with the integer being the width of the triangle's bottom
or the number of asterisks of the bottom row. For instance, if the integer is 7, you would
display the triangle below:
***
*****
Or if the integer is not positive or not odd, do nothing and continue to read another integer.
4. A segment of an array is defined as a collection of consecutive elements in that array. The length
of a segment is the number of elements in the segment. A repeating segment is a segment
with all the elements in it having the same value. The following method returns the length
of the longest repeating segment in a given int array. Complete the implementation of the
method.
public int getLength0fLongestRepeatingSegment (int [] array) {
// add your implementation here
2
}
3](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F93f9cbfc-8de6-44b0-bf57-24b8a06d5fd9%2Fe7c31efe-8570-4c30-82cd-1fb8d1058790%2F8v3qq8o_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 3 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)