Search⌘ K
AI Features

Solution Review: Printing Styles

Explore how to effectively use Java's print and println methods to display text and variables with proper formatting. Learn the distinctions between printing with and without line breaks, and discover common pitfalls such as spacing errors. This lesson helps you master basic Java output styles essential for clear and correct program output.

Rubric criteria

Solution

Java
class Announcements
{
public static void men100meters(float time, String month, int year)
{
System.out.println("Usain Bolt has broken the world record for men's 100 meters.");
System.out.println( "" );
System.out.print("Time taken:");
System.out.println(time);
System.out.print("Month:");
System.out.println(month);
System.out.print("Year:");
System.out.println(year);
}
// testing code
public static void main(String args[])
{
men100meters(9.58f, "August", 2009);
}
}

Rubric-wise explanation


Point 1:

The first line, i.e., Usain Bolt has broken the world record for men's 100 meters., is simply printed using println because we need it to end with the line termination (see line 5). We need another empty line. Thus, we call println with an empty text literal at line 6. Notice that having a " " instead of "" here ...