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 (" \tName\t\tHometown") ;
System.out.println ("\t====\t\t========");
System.out.println ("\tSally\t\tRoanoke");
System.out.println ("\tAlexander\tWashington")
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!).
//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 (" \tName\t\tHometown") ;
System.out.println ("\t====\t\t========");
System.out.println ("\tSally\t\tRoanoke");
System.out.println ("\tAlexander\tWashington");
//add two more student sitting near you
System.out.println ("\tJohn\t\tNewyork");
System.out.println ("\tSamanth\t\tBoston");
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 (" \tName\t\tHometown\tMajor") ;
System.out.println ("\t====\t\t========\t=====");
System.out.println ("\tSally\t\tRoanoke\t\tComputer
science");
System.out.println ("\tAlexander\tWashington\tMath");
//add two more student sitting near you
System.out.println ("\tJohn\t\tNewyork\t\tElectronics");
System.out.println ("\tSamanth\t\tBoston\t\tSocial");
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
Get Answers For Free
Most questions answered within 1 hours.