Respuesta :
Answer:
Here is code CarlysMotto.java
//import package
import java.util.*;
// class name
class CarlysMotto
{
// main method of class CarlysMotto
public static void main (String[] args) throws java.lang.Exception
{
try{
// string to hold the Carly's motto
String motto="Carly’s makes the food that makes it a party.";
System.out.println("Carly’s motto: ");
// print the motto
System.out.println(motto);
}catch(Exception ex){
return;}
}
}
Explanation:
Create a class named "CarlysMotto".In the main method, create a strint that holds the motto.In the new line print "Carly’s motto:".After that print the string that holds Carly’s motto in newline.
Here is next code CarlysMotto2.java
//import package
import java.util.*;
// class name
class CarlysMotto2
{
// main method of class CarlysMotto
public static void main (String[] args) throws java.lang.Exception
{
try{
// string to hold the Carly's motto
String motto="Carly’s makes the food that makes it a party.";
System.out.println("Carly’s motto: ");
// this will print a line of asterisks(*) System.out.println("***********************************************************");
// print the motto
System.out.println(motto);
// this will print a line of asterisks(*) System.out.println("***********************************************************");
}catch(Exception ex){
return;}
}
}
Explanation:
Create a class named "CarlysMotto".In the main method, create a strint that holds the motto.In the new line print "Carly’s motto:".Then print a line of asterisks (*).After that print the string that holds Carly’s motto in newline.After then print a line of asterisks(*).
Output:
Here is the output of both the program.
Answer:
CarlysMotto.java
- public class Main {
- public static void main(String[] args) {
- System.out.println("Carly’s makes the food that makes it a party.");
- }
CarlysMotto2.java
- public class Main {
- public static void main(String[] args) {
- String motto = "Carly’s makes the food that makes it a party.";
- for(int i=0; i < motto.length() + 2; i++){
- System.out.print("*");
- }
- System.out.println();
- System.out.print("*" + motto + "*");
- System.out.println();
- for(int i=0; i < motto.length() + 2; i++){
- System.out.print("*");
- }
- }
- }
Explanation:
CarlysMotto.java
To display the motto, we can simply write a print statement using the println() method (Line 3).
CarlysMotto2.java
To print the "asterisks" border that surround the motto string, we need to estimate the number of asterisks needed to build the top and bottom borderline. The number of asterisks will be equal to the length of the motto string + 2.
Based on this understanding, we can create the for-loop by iterating motto length + 2 times to print the asterisks to form the top and bottom borders (Line 6 - 8 and Line 16 - 18).
In the middle, we can simply concatenate an asterisk with motto string and with a asterisk again (Line 12).
The output shall look like this:
*********************************************** ************
*Carly’s makes the food that makes it a party.*
***********************************************************