Sometimes we create a distinct .java file for each of theseclasses. We don\'t al
ID: 3616518 • Letter: S
Question
Sometimes we create a distinct .java file for each of theseclasses. We don't always have to do this, however. In thisassignment, for example, you will be creating two classeswithin the same file.
First, though, here is an important peice of information for youabout Java classes: When two or more classes are contained in thesame file, at most one class can be specified as"public." No other class contained in the same file can bespecified as "public." For this assignment, create only theclass MyProgram (further described below) as a 'public' class(Feel free to experiment on your own, however, to see what wouldhappen if you should create none or both of these classes as'public' and otherwise followed the directions below. Just don'tmake the mistake of turning in these experiments!)
Using the filename, MyProgram.java, create a class calledSayGoodby. In the same file, after theend of the class SayGoodby, create a public classcalled MyProgram.
and
The method FarewellSaying should create an object of typeString, and label it with the variable name mystringlabel.FarewellSaying will use mystringlabel both to store andthen to return the exact String (includingpunctuation)
The method ShowReference should print the reference ofan object of the SayGoodby class. This means that the printstatement doing this should be included in the ShowReference methodand not elsewhere.
As we will have discussed in class, because the MyProgram classuses the SayGoodby class, in order to compile both parts of yourprogram, you need only use the following command:
If the compiler execution is successful, your directory shouldthen contain (among others) the file
To test execution of your program, of course, you would then usethe command:
Upload the following file:
MyProgram.java
Explanation / Answer
import java.io.*; class SayGoodby { public String FarewellSaying() { String mystringlabel = "Tis afond adieu I bid you."; return mystringlabel; } public void ShowReference() { System.out.println(toString()); } } public class MyProgram{ public static void main(String [] args) { SayGoodby talker = newSayGoodby(); System.out.println("*****************************"); talker.ShowReference(); System.out.println(talker.FarewellSaying()); System.out.println("*****************************"); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.