Java: public class SecondProgram{ /*Print a message to the console, instructing
ID: 3872768 • Letter: J
Question
Java:
public class SecondProgram{
/*Print a message to the console, instructing the user to type a wise message for an owl to say.
• Collect that message into an appropriately named String variable.
• Declare and initialize a single Scanner object. Use its nextLine() method to capture the user’s
input string from the console.
• Based on the length of the message, print out the line of stars for the top and bottom
border of the text box, using a for-loop. Print the message with a single straight line
character on either side, leaving a space between the first and final characters of the
message.
• Add the call to printMessage inside the printOwl method. Note that this is the only
modification necessary to the Assignment One version of printOwl.
• Test printMessage inside the main method; once you see it works fine, then you can
test printOwl to see the complete drawing.*/
public static void printMessage(){
}
public static void printOwl(){
System.out.println(" ^...^");
System.out.println(" / o,o \");
System.out.println(" |):::(|");
System.out.println(" ===w=w===");
}
/*Instead of calculating the first 8 terms of the sequence, use the first 1, 000, 000 terms.
You must use a for-loop to do this.
• Remembering that the formula for a circle is r2,
where r is the radius of the circle, ask
the user to provide the radius. The code for this action
is similar to the printMessage
method, where you created a Scanner object and asked the user for the value. In this
case, the value we want is a number, a double in fact, so instead of using the Scanner’s
nextLine() method, use nextDouble() instead.
• Calculate the area of the circle where the radius is the user’s input and is the value
poduced by the million iterations of the series.
Print this value to the console.
• Test the areaCircle method by adding a call in the main method.*/
public static void areaCircle(){
float nextTerm = 1;
float denom = 1;
float series = 0;
int i = 0;
while(i < 8){ //Loop 8 times
series = series + (nextTerm/denom);
denom = denom + 2;
nextTerm = nextTerm* -1;
i++;
}
series = series * 4;
System.out.println(series);
}
public static void main(String[] args){
}
}
Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
printOwl();
}
public static void printMessage(){
//*Print a message to the console, instructing the user to type a wise message for an owl to say.
System.out.println("Type a wise message for an owl to say");
Scanner scan = new Scanner(System.in);
String owlMessage = scan.nextLine();
int length = owlMessage.length();
System.out.print(" ");
for(int i=0; i<length+4; i++){
System.out.print("*");}
System.out.println(" ");
System.out.print(" ");
System.out.println("- "+owlMessage+" -");
System.out.print(" ");
for(int i=0; i<length+4; i++){
System.out.print("*");}
System.out.println(" ");}
public static void printOwl(){
printMessage();
System.out.println(" ^...^");
System.out.println(" / o,o \");
System.out.println(" |):::(|");
System.out.println(" ===w=w===");
}
}
88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
printOwl();
areaCircle();
}
public static void printMessage(){
//*Print a message to the console, instructing the user to type a wise message for an owl to say.
System.out.println("Type a wise message for an owl to say");
Scanner scan = new Scanner(System.in);
String owlMessage = scan.nextLine();
int length = owlMessage.length();
System.out.print(" ");
for(int i=0; i<length+4; i++){
System.out.print("*");}
System.out.println(" ");
System.out.print(" ");
System.out.println("- "+owlMessage+" -");
System.out.print(" ");
for(int i=0; i<length+4; i++){
System.out.print("*");}
System.out.println(" ");}
public static void printOwl(){
printMessage();
System.out.println(" ^...^");
System.out.println(" / o,o \");
System.out.println(" |):::(|");
System.out.println(" ===w=w===");
}
public static void areaCircle(){
System.out.println("Enter the radius of circle to calculate area");
Scanner scan = new Scanner(System.in);
float radius = scan.nextFloat();
float nextTerm = 1;
float denom = 1;
float series = 0;
int i = 0;
while(i < 1000000){ //Loop 8 times
series = series + (nextTerm/denom);
denom = denom + 2;
nextTerm = nextTerm* -1;
i++;
}
series = series * 4;
System.out.println("Value of pi = "+series);
float area = series*radius*radius;
System.out.println("Area of circle = "+ area);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.