Wednesday, 18 September 2013

Beginner Java Program to repeat user phrase

Beginner Java Program to repeat user phrase

My first post, so be gentle.
I am in an Intro to Programming class. We are covering loops after doing
the variables, classes, getters/setters, and constructors. I understand
how loops work. However, My confusion is in the directions. I have the
bare bones set up, but the instructions confuse me as I am trying to
figure out putting a method within another (if I understand correctly).
The directions are as follows:
UML Class Diagram: Learning Outcomes: „h Declare a class that is intended
to be instantiated „h Declare a method to provide a functionality „h
Create an instance of the class you declared „h Access the method using
the dot operator „h Increase your familiarity with UML class diagrams „h
Review the use of Scanner „h Create a project called LabParrot „h Add 2
files to the project: Parrot.java and ParrotTest.java „h In Parrot.java do
the following: „X Create a public class called Parrot „X Inside the class
create a public method called speak. The method speak has one String
parameter named word and no return value (i.e. return type void) The
method header looks like this: public void speak(String word) „X The
parrot repeats anything he is told. We implement this behavior by printing
the word passed as an argument. „h In ParrotTest.java create the main
method Inside the main method do the following: „X Create an instance of
Scanner named input „X Create an instance of Parrot named myParrot You
create a new instance by calling the default constructor like this: Parrot
myParrot = new Parrot(); „X Read in a text (i.e. use Scanner to let the
user choose what s/he would like to say to the parrot); make sure to
prompt the user before you read in the text Create a String variable
called text to temporarily store the input read „X Call the method speak
of the instance myParrot and pass the variable text as argument NOTE: the
variable name passed does not have to match the parameter name The method
call looks like this: myParrot.speak(text); Sample Output1: What would you
like to say to the parrot? hi hi Sample Output2: What would you like to
say to the parrot? how are you? how are you?
My code so far is this:
import java.util.Scanner;
public class LabParrot {
//begin method public void static void(String [] args) {
//set up variables
char String = word;
Scanner input = new Scanner(System.in);
//Obtain user input
System.out.println("What would you like to say to the parrot?");
word = input.nextLn();
}
}
Did I put everything in the correct place so far? Sorry if this seems
silly. I don't have too many similar examples to this assignment to figure
it out.

No comments:

Post a Comment