Question

Getting started with Java on elvis Download Greeting.java from the class web site. Use FileZilla to...

Getting started with Java on elvis

Download Greeting.java from the class web site. Use FileZilla to place it into your Lab5 directory.

Look at the content of your directory to see the file using the command ls

Look at the content of the file in your directory using the command more Greeting.java

Compile the HelloClass program using the command javac Greeting.java. Then use ls to see your class file.

Run the program without parameters using the command java Greeting

Run the program a second time with two names as parameters.

Run the program a third time with the same two names, only this time use file indirection to send the output to file greeting.txt. What did you notice about System.in? Include greeting.txt in your lab submission.

Download and run Greeting2.java.

//Greeting.java

import java.util.Scanner;

/**
 * This is the first incarnation of Greeting.
 * It demonstrates how to pass arguments to main method
 * 
 * @author myersjac
 *
 */
public class Greeting {

        public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
                try { 
                String myName = args[0];
                String yourName = args[1];
                System.out.println("Are you sure you want to say hello to " + yourName+ "?");
                if (sc.nextLine().toLowerCase().equals("no")) {
                        System.out.println("Who would you rather say hello to?");
                        yourName = sc.next();
                }
                System.out.println(myName + " says hello to " + yourName);
        }
        catch (ArrayIndexOutOfBoundsException e) {
                System.out.println("Hello!");
        }
        finally {
                sc.close();
        }
    }
}

//Greeting2.java

import java.util.ArrayList;
import java.util.Scanner;

/**
 * This is the second incarnation of Greeting
 * It prompts for a number of people who will greet the class
 * 
 * @author myersjac
 *
 */
public class Greeting2 {

        public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter number of people: ");
        ArrayList<String> names = new ArrayList<String>();
        int numberOfPeople = Integer.parseInt(sc.nextLine());
        int i;
        for (i = 0; i < numberOfPeople; i++) {
                System.out.printf("Enter name #%d: ", i+1);
                names.add(sc.nextLine());
        }
        String nameString = "";
        for (String name : names) {
                nameString += name + ", ";
        }
        // Kill terminal ", "
        nameString = nameString.substring(0, nameString.lastIndexOf(","));
        // Get up to the last name
        nameString = nameString.substring(0, nameString.lastIndexOf(" ")) +
                             " and " + 
                             nameString.substring(nameString.lastIndexOf(" ")+1, nameString.length());
        System.out.println(nameString + (i==1 ? " says" : " say") + " Hello Class!!!");
        sc.close();
        }
}
0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
Getting started with Java on elvis Download Greeting.java from the class web site. Use FileZilla to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • In Java This is the method we did in class. How do I reverse the string...

    In Java This is the method we did in class. How do I reverse the string "monday"? If I could get the string "onday" reversed (to produce "yadno" then all I have to do is append the first character to make "yadnom". import java.util.Scanner; public class Lab12Num2 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String s=sc.nextLine(); System.out.println("The string entered was " + s); System.out.println("The string printed backwards is "+ printBackwards(s)); }    public static String printBackwards(String one)...

  • I am currently doing homework for my java class and i am getting an error in...

    I am currently doing homework for my java class and i am getting an error in my code. import java.util.Scanner; import java.util.Random; public class contact {       public static void main(String[] args) {        Random Rand = new Random();        Scanner sc = new Scanner(System.in);        System.out.println("welcome to the contact application");        System.out.println();               int die1;        int die2;        int Total;               String choice = "y";...

  • Modify the below Java program to use exceptions if the password is wrong (WrongCredentials excpetion). import java.util....

    Modify the below Java program to use exceptions if the password is wrong (WrongCredentials excpetion). import java.util.HashMap; import java.util.Map; import java.util.Scanner; class Role { String user, password, role; public Role(String user, String password, String role) { super(); this.user = user; this.password = password; this.role = role; } /** * @return the user */ public String getUser() { return user; } /** * @param user the user to set */ public void setUser(String user) { this.user = user; } /** *...

  • Q22 Consider the following 2 text files and Java program. 8 Points Answer the following questions...

    Q22 Consider the following 2 text files and Java program. 8 Points Answer the following questions keeping in mind that: • All three files are in the same directory, and there are no other files. • The program may crash. If the program crashes, answer the question with: "The program crashes" standings1.txt: 1 Valtteri Bottas 25 2 Charles Leclerc 18 3 Lando Norris 16 4 Lewis Hamilton 12 standings2.txt: 1 Valtteri Bottas 43 2 Lewis Hamilton 37 3 Lando Norris...

  • Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In...

    Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and cannot figure it out. Thank you. Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and...

  • composed the following java code to read a string from a text file but receiving compiling...

    composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main {   public static void main(String[] args) {     System.out.print("Enter the...

  • Can someone compile this and name the it A4MA5331550.java and compile to make .class file. Will...

    Can someone compile this and name the it A4MA5331550.java and compile to make .class file. Will need to name . class file A4MA5331550 also when done put both files in a zipped folder named A4MA5331550 and email the folder to [email protected] here is the program: import java.util.Scanner; /** * 09/17/2017 * Dakota Mammedaty * MA5331550 * Bubble sorted */ public class MA5331550 { public static void main(String[] args) throws IOException { Sort st =new Sort(); st.getData(); System.out.println("=================Sorting Algorithms================="); System.out.println("1. Bubble...

  • Filename(s): ReformatCode. java Public class: ReformatCode Package-visible class(es): none Write a program that reformats Java source...

    Filename(s): ReformatCode. java Public class: ReformatCode Package-visible class(es): none Write a program that reformats Java source code from the next-line brace style to the end-of-line brace style. The program is invoked from the command line with the input Java source code file as args [0] and the name of the file to save the formatted code in as args [1]. The original file is left untouched. The program makes no other changes the source code, including whitespace. For example, the...

  • make this program run import java.util.Scanner; public class PetDemo { public static void main (String []...

    make this program run import java.util.Scanner; public class PetDemo { public static void main (String [] args) { Pet yourPet = new Pet ("Jane Doe"); System.out.println ("My records on your pet are inaccurate."); System.out.println ("Here is what they currently say:"); yourPet.writeOutput (); Scanner keyboard = new Scanner (System.in); System.out.println ("Please enter the correct pet name:"); String correctName = keyboard.nextLine (); yourPet.setName (correctName); System.out.println ("Please enter the correct pet age:"); int correctAge = keyboard.nextInt (); yourPet.setAge (correctAge); System.out.println ("Please enter the...

  • I need help debugging this Java program. I am getting this error message: Exception in thread...

    I need help debugging this Java program. I am getting this error message: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at population.Population.main(Population.java:85) I am not able to run this program. ------------------------------------------------------------------- import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; /* Linked list node*/ class node { long data; long year; String country; node next; node(String c,long y,long d) { country=c; year=y; data = d; next = null; } } public class Population { private static node head; public static void push(String...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT