Java Lab1 Object-Oriented Hello World In this lab we will be building a simple o
ID: 3872460 • Letter: J
Question
Java
Lab1
Object-Oriented Hello World
In this lab we will be building a simple object-oriented “hello world”. Declare a single class called
HelloWorld according to the following UML class diagram:
In addition to this class, a second class containing a main method should be declared. This
second class is called the app class; this is because the main method drives the app when the
class that contains it is compiled and run. You may declare both the hello world class and the
app class in a single file. If you do, remember that there can only be one public class in each file
and that the file must bear the name of the public class that is declared in it.
Before writing any code, use software such as Microsoft Visio or
draw.io
to create a UML class
diagram depicting the hello world class (like the one pictured above). There is no need to create
a UML class diagram for the app class.
Create pseudocode, describing purpose, input, processing and output. Each of these
descriptions need only be one to two sentences.
Once the logical specification (UML class diagram and pseudocode) is complete, then and only
then move on to coding up the hello world class and the app class. Build incrementally and test.
Make the tests as specific as possible. Testing requires identifying what is being tested, forming
a hypothesis (what will the code that’s being tested do when it is run?) compiling/running and
observing the results. If one’s hypothesis does not accord with the way the program runs then
the code must be revised and retested. Sometimes the logical specification must be revised; if
for example the implementation diverges from the logical specification.
HelloWorld message: String + setMessage(m: String): void +getMessage(): StringExplanation / Answer
Java code:
Create a new file app.java and paste following code into it! filename must be app.java
app.java
public class app {
public static void main(String[] args)
{
HelloWorld tmp = new HelloWorld();
String out = tmp.getmessage();
System.out.println(out);
tmp.setmessage("Hellow World 2");
out = tmp.getmessage();
System.out.println(out);
}
}
Create a new file HelloWorld.java and paste following code into it! filename must be HelloWorld.java
HelloWorld.java
public class HelloWorld {
public String message = "Hello World";
public void setmessage (String s)
{
this.message = s;
}
public String getmessage()
{
return this.message;
}
}
You need to run app.java only
Sample Output:
Hello World
Hellow World 2
Psuedocode app.java:
HelloWorld tmp = new HelloWorld(); // Declair an instance of HelloWorld class
String out = tmp.getmessage(); // Call getmessage() function of HelloWorld class
System.out.println(out); // print the message replied by getmessage() function
tmp.setmessage("Hellow World 2"); // Assign new value to the message varible a= in HelloWorld class
out = tmp.getmessage(); // Call getmessage() function of HelloWorld class
System.out.println(out); // print the message replied by getmessage() function
Psuedocode app.java:
public void setmessage (String s)
{
this.message = s; //reset value of string variable message
}
public String getmessage()
{
return this.message; //return value of string variable message
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.