Complete the implementation of the static methods described below in a single cl
ID: 3784804 • Letter: C
Question
Complete the implementation of the static methods described below in a single class, Assignment1.java. Create a second class, Assignment1Tests.java, to test your methods with a main method that calls each of the methods you wrote. For each method, print a line for the name of the method being tested and at least two examples of sample input and output. Add documentation at the class level and at the method level using JavaDoc style to both classes.
1. comment()
String return type
one int parameter representing an age
returns one of six comments depending upon the age
< 0: You have not been born yet
0 ... 15: You are too young to drive
16 ... 17: You are old enough to drive
18 ... 20: You are old enough to vote
21 ... 116: You are old enough to drink responsibly
117: You set the record as the oldest person
2. print()
void return type (i.e., does not return a value)
three parameters: a String and two ints.
action: the method prints its String parameter repeatedly. Its first int parameter specifies the number of lines to be printed; the second int parameter specifies the number of copies of the string to be printed per line.
For example, if the String was "golly", the first int parameter was 3 and the second int parameter was 2, this would be the output:
gollygolly
gollygolly
gollygolly
3. nextColor()
Color return type
no parameters
returns a Color with random RGB (red, green, blue) values
approach: generate three random numbers representing valid RGB levels (0 to 255), produce a color using the random RGB levels.
You will need to include an import statement at the beginning of your class in order to use the Color class (import java.awt.Color;)
Color objects can be printed with System.out.println and will give output similar to the following:
java.awt.Color[r=110,g=243,b=101]
4. Answer the following questions about the methods you wrote:
Give an example from your code in Assignment1Tests.java of the formal parameter and the actual parameter for comment()
Which method above, if any, includes a parameter passed by reference? Why is it passed by reference or why isn't there one?
If you were asked to write an overloaded version of the print() method that assumed the number of lines and number of copies were each 10, what would this version of print()'s method heading (method signature) look like?
Explanation / Answer
Assignment1Tests.java
import java.awt.Color;
public class Assignment1Tests
{
public static void main(String[] args)
{
Assignment1 f = new Assignment1();
System.out.println("1. comment() ");
String s;
s = f.comment(15);
System.out.println(s);
s = f.comment(117);
System.out.println(s);
System.out.println("2. print() ");
f.print("golly",3,2);
System.out.println("3. nextColor() ");
Color c = f.nextColor();
System.out.println(c);
}
}
Assignment1.java
import java.util.*;
import java.awt.Color;
public class Assignment1
{
public Assignment1()
{
}
public Color nextColor()
{
// System.out.println(java.awt.Color[r=110,g=243,b=101]);
Color c ;
Random rand = new Random();
int r = rand.nextInt(255) + 1;
int g = rand.nextInt(255) + 1;
int b = rand.nextInt(255) + 1;
c = new Color(r,g,b);
return c;
}
public String comment(int a)
{
String s = "";
if(a < 0)
{
s = "You have not been born yet";
}
else if(a >= 0 && a<=15)
{
s = "You are too young to drive";
}
else if(a >= 16 && a<=17)
{
s = "You are old enough to drive";
}
else if(a >= 18 && a<=20)
{
s = "You are old enough to vote";
}
else if(a >= 21 && a<=116)
{
s = "You are old enough to drink responsibly";
}
else if(a >= 117)
{
s = "You set the record as the oldest person";
}
return s;
}
public void print(String s, int a, int b)
{
int i = 0;
while(i < a)
{
int j = 0;
while(j < b)
{
System.out.print(s);
j++;
}
System.out.print(" ");
i++;
}
}
}
Part 4)
a)
comment(15)
You are too young to drive
comment(117)
You set the record as the oldest person
b)
There is no method above, which includes a parameter passed by reference. Beacuse,Java doesn't pass method arguments by reference; it passes them by value
c)
modified print() methode:
public void print(String s)
{
int i = 0;
while(i < 10)
{
int j = 0;
while(j < 10)
{
System.out.print(s);
j++;
}
System.out.print(" ");
i++;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.