Question

In: Computer Science

IN JAVA 12.11 PRACTICE: Branches*: Listing names A university has a web page that displays the...

IN JAVA

12.11 PRACTICE: Branches*: Listing names

A university has a web page that displays the instructors for a course, using the following algorithm: If only one instructor exists, the instructor's first initial and last name are listed. If two instructors exist, only their last names are listed, separated by /. If three exist, only the first two are listed, with "/ …" following the second. If none exist, print "TBD". Given six words representing three first and last names (each name a single word; latter names may be "none none"), output one line of text listing the instructors' names using the algorithm. If the input is "Ann Jones none none none none", the output is "A. Jones". If the input is "Ann Jones Mike Smith Lee Nguyen" then the output is "Jones / Smith / …".

Hints:

  • Use an if-else statement with four branches. The first detects the situation of no instructors. The second one instructor. Etc.

  • Detect whether an instructor exists by checking if the first name is "none".

CODE GIVEN:

import java.util.Scanner;

public class main {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String firstName1, lastName1;
String firstName2, lastName2;
String firstName3, lastName3;
  
firstName1 = scnr.next();
lastName1 = scnr.next();
firstName2= scnr.next();   
lastName2= scnr.next();
firstName3= scnr.next();
lastName3= scnr.next();
  
/* Type your code here. */
  
}
}

Solutions

Expert Solution

Code:

import java.util.Scanner;

public class main {
public static void main(String[] args) {
  
Scanner scnr = new Scanner(System.in);
String firstName1, lastName1;
String firstName2, lastName2;
String firstName3, lastName3;
  
firstName1 = scnr.next();
lastName1 = scnr.next();
firstName2= scnr.next();   
lastName2= scnr.next();
firstName3= scnr.next();
lastName3= scnr.next();
  
/* Type your code here. */
  
// case 1 : none exists
if( firstName1.equals("none")){
System.out.println("TBD");
}
// case 2: one instructor exists
else if( firstName2.equals("none") ) {
System.out.println(firstName1.charAt(0) + "." + lastName1);
}
// case 3: two instructor exists
else if( firstName3.equals("none") ) {
System.out.println(lastName1 + "/" + lastName2);
}
// case 4: all three instructor exists
else{
System.out.println(lastName1 + "/" + lastName2 + "/...");
}
  
}

==================

Screenshot:

Sample Output:

================

Explanation:

The case of no instructor is true if firstName1 is none. So we print "TBD".

If the firstName2 is none it means we only have one instructor

If firstName3 is none means we have two instructor

Other wise all three instructors are present

================

Please upvote.

For any query comment.


Related Solutions

3.32 Prog4-Branches-Listing names A university has a web page that displays the instructors for a course,...
3.32 Prog4-Branches-Listing names A university has a web page that displays the instructors for a course, using the following algorithm: If only one instructor exists, the instructor's first initial and last name are listed. If two instructors exist, only their last names are listed, separated by /. If three exist, only the first two are listed, with "/ …" following the second. If none exist, print "TBD". Given six words representing three first and last names (each name a single...
You are designing a web page that allows users to create an event listing. The event...
You are designing a web page that allows users to create an event listing. The event listing should include the date, time, location, title, phone, email, coordinator, and description of the event. The location should be between 10 and 100 characters long. The title should be between 1 and 50 characters long. The description should be between 10 and 200 characters long. Phone number consists of numbers and dashes. Email has to have an @ symbol. All data elements should...
You are designing a web page that allows users to create an event listing. The event...
You are designing a web page that allows users to create an event listing. The event listing should include the date, time, location, title, phone, email, coordinator, and description of the event. The location should be between 10 and 100 characters long. The title should be between 1 and 50 characters long. The description should be between 10 and 200 characters long. Phone number consists of numbers and dashes. Email has to have an @ symbol. All data elements should...
Create a web page using PHP and HTML that contains a list of movie names available...
Create a web page using PHP and HTML that contains a list of movie names available for rent (at least 10 movies), so that each time you visit the home page, you see a different order of movies in the list. The movie names are required. 2) The rental price for each movie ranges between $1.99 to $7.99. • Create an associative array with a code for the movie as the key (e.g., movie1, movie2, etc.) and the associated rental...
use CSS, java and HTML to make a charity admin page. It's a web page after...
use CSS, java and HTML to make a charity admin page. It's a web page after you log in to your account. I can have personal information, the amount of money donated, the children who have received charity, and some blogs. In fact, all the things are what I want. But you are free to do whatever You like, even if you don't say what I say. I just need a charity Admin page for charity User.Don't ask me for...
use CSS, java and HTML to make a charity admin page. It's a web page after...
use CSS, java and HTML to make a charity admin page. It's a web page after you log in to your account. I can have personal information, the amount of money donated, the children who have received charity, and some blogs. In fact, all the things are what I want. But you are free to do whatever You like, even if you don't say what I say. I just need a charity Admin page for charity User.Don't ask me for...
use CSS, java and HTML to make a charity admin page. It's a web page after...
use CSS, java and HTML to make a charity admin page. It's a web page after you log in to your account. I can have personal information, the amount of money donated, the children who have received charity, and some blogs. In fact, all the things are what I want. But you are free to do whatever You like, even if you don't say what I say. I just need a charity Admin page for charity User.Don't ask me for...
use CSS, java and HTML to make a charity admin page. It's a web page after...
use CSS, java and HTML to make a charity admin page. It's a web page after you log in to your account. I can have personal information, the amount of money donated, the children who have received charity, and some blogs. In fact, all the things are what I want. But you are free to do whatever You like, even if you don't say what I say. I just need a charity Admin page for charity User.Don't ask me for...
use CSS, java and HTML to make a charity admin page. It's a web page after...
use CSS, java and HTML to make a charity admin page. It's a web page after you log in to your account. I can have personal information, the amount of money donated, the children who have received charity, and some blogs. In fact, all the things are what I want. But you are free to do whatever You like, even if you don't say what I say. I just need a charity Admin page for charity User.Don't ask me for...
use CSS, java and HTML to make a charity admin page. It's a web page after...
use CSS, java and HTML to make a charity admin page. It's a web page after you log in to your account. I can have personal information, the amount of money donated, the children who have received charity, and some blogs. In fact, all the things are what I want. But you are free to do whatever You like, even if you don't say what I say. I just need a charity Admin page for charity User.Don't ask me for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT