The goal in this exercise is to develop a program that will print out a list of
ID: 3787152 • Letter: T
Question
The goal in this exercise is to develop a program that will print out a list of student names together with other information foreach. The tab character
(an escape sequence) is helpful in getting the list to line up nicely. A program with only two names is in the file Names.java.
// ************************************************************
// Names.java
//
// Prints a list of student names with their hometowns
// and intended major
// ************************************************************
public class Names
{
// --------------------------
//
main prints the list
// --------------------------
public static void main (String[] args)
{
System.out.println ();
System.out.println (" Name Hometown") ;
System.out.println (" ==== ========");
System.out.println (" Sally Roanoke");
System.out.println (" Alexander Washington")
System.out.println ();
}
}
1. Save Names.java to your directory. Compile and run it to see how it works.
2. Modify the program so that your name and hometown and the name and hometown of at least two classmates sitting near
you in lab also are printed. Save, compile and run the program. Make sure the columns line up.
3. Modify the program to add a third column with the intended major of each person (assume Sally's major is Computer
Science and Alexander's major is Math). Be sure to add a label at the top of the third column and be sure everything is
lined up (use tab characters!).
Explanation / Answer
//names.java file content
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class names{
public static void main(String []args){
System.out.println ();
System.out.println (" Name Hometown") ;
System.out.println (" ==== ========");
System.out.println (" Sally Roanoke");
System.out.println (" Alexander Washington");
//add two more student sitting near you
System.out.println (" John Newyork");
System.out.println (" Samanth Boston");
System.out.println ();
}
}
if executing in linux env , first compile
javac names.java
then execute using java names
//output of the program after adding two more names
Name Hometown
==== ========
Sally Roanoke
Alexander Washington
John Newyork
Samanth Boston
---------------------------------------------------------------------------
//program to add one more column major
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class names{
public static void main(String []args){
System.out.println ();
System.out.println (" Name Hometown Major") ;
System.out.println (" ==== ======== =====");
System.out.println (" Sally Roanoke Computer science");
System.out.println (" Alexander Washington Math");
//add two more student sitting near you
System.out.println (" John Newyork Electronics");
System.out.println (" Samanth Boston Social");
System.out.println ();
}
}
--------------------------------------------------------
output of second program
sh-4.3$ javac names.java
sh-4.3$ java -Xmx128M -Xms16M names
Name Hometown Major
==== ======== =====
Sally Roanoke Computer science
Alexander Washington Math
John Newyork Electronics
Samanth Boston Social
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.