8.22222222222222222222222222222 Thanks everybody for your help out there, will r
ID: 3803972 • Letter: 8
Question
8.22222222222222222222222222222
Thanks everybody for your help out there, will rate your amazing works.
part 1
Begin by creating a new Java project
Create a new Java class inside your project folder.
The name of the class should be: Lines
Write a program that will display a series of asterisks across the screen. The number of asterisks that you should print will be entered by the user.
You should begin the program by asking the user how many asterisks to print. After retrieving this number, your program should print the requested number of asterisks, all on the same line. Make sure that the "line-break" character is printed after the last asterisk.
For example, if the user enters 5, then your program should display this:
*****
If the user enters 2, then your program should display this:
**
If the user enters 10, then your program should display this:
**********
Program Restrictions
The main program should only do two major things:
Get the input from the user.
Use a method named printLine to display the output on the screen.
You must write and use a method called printLine. This method IS NOT ALLOWED to get any values from the user/keyboard – that task should be handled by the main program. This method should be given an integer value from the main program. This integer tells the method how many asterisks should be printed to the screen. The method, then, should simply print the requested number of asterisks, followed by a line break character.
The main program is NOT ALLOWED to print any asterisks to the screen. It MUST call on the printLine method, and let the printLine method take care of the printing the asterisks.
Declare the keyboard object as a class (global) variable as discussed in our class example. You are NOT allowed to use any other class (global) variables in this program.
All other variables must be declared as local.
I am mostly interested in part 2, but to do so I need to use the part one method so if you make it work and concentrate on part two that will be fine
part 2
Create a new Java class inside your project folder.
The name of the class should be: Triangle
Write a program that asks the user to enter the size of a triangle to print on the screen. After getting this number, the program should then print a triangle to the screen by printing a series of lines consisting of asterisks. The first line will have one asterisk. The second line will have two asterisks, and so on, with each line having one more asterisk than the previous line, up to the number entered by the user. After reaching the number entered by the user, on the next line, print one less asterisk and continue by decreasing the number of asterisks by one for each successive line until only one asterisk is printed.
For example, if the user enters 3, then your program should display this:
*
**
***
**
*
If the user enters 5, then your program should display this:
*
**
***
****
*****
****
***
**
*
Program Requirements
Begin this part of the assignment by placing a copy of your printLine method from Part 1 into this program. You will use this method to help you with this part of the assignment. This method should be an EXACT COPY from Part 1. DO NOT change the printLine method when you copy it into your program for Part 2 of this assignment.
The main program should only do two major things:
Get the input from the user.
Use a method named printTriangle to display the output on the screen.
You must write and use a method called printTriangle. This method should NOT get any values from the user/keyboard – that task should be performed by the main program. This method should be sent an integer value from the main program. This integer tells the method how many asterisks should be in the longest line of the triangle. The method, then, should use two loops to repeatedly call on the printLine method. The loops essentially keep changing the value of the integer that is sent to the printLine method so that each time the printLine method prints a different line length. You will need one loop to count up (making the lines longer and longer). When this loop ends, you will need to go into another loop which counts down (making the lines smaller and smaller).
The main program is NOT allowed to print any asterisks to the screen. It MUST call on the printTriangle method, and let that method take care of the printing the triangle to the screen.
The printTriangle method is NOT allowed to print any asterisks to the screen. It MUST use the printLine method repetively to print the lines of asterisks to the screen.
Declare the keyboard object as a class (global) variable as discussed in our class example. You are NOT allowed to use any other class (global) variables in this program.
All other variables must be declared as local.
Explanation / Answer
Part 1)
Lines.java
import java.util.Scanner;
public class Lines {
//Declaring global variable
static Scanner keyboard=new Scanner(System.in);
public static void main(String[] args) {
//Getting the number entered by the user
System.out.print("Enter the number :");
int number=keyboard.nextInt();
/* Calling the method by passing
* the user entered number as argument
*/
printLine(number);
}
//This method will print the line of asterisks
private static void printLine(int number) {
for(int i=1;i<=number;i++)
System.out.print("*");
System.out.print(" ");
}
}
_____________________
Output:
Enter the number :5
*****
_____________________
Part 2)
Triangle.java
import java.util.Scanner;
public class Triangle {
//Declaring global variable
static Scanner keyboard=new Scanner(System.in);
public static void main(String[] args) {
//Getting the number entered by the user
System.out.print("Enter the number :");
int number=keyboard.nextInt();
/* calling the function by passing
* the user entered number as argument
*/
printTriangle(number);
}
//This function will print the triangle
private static void printTriangle(int number) {
for(int i=1;i<=number;i++)
printLine(i);
for(int i=number-1;i>=1;i--)
printLine(i);
}
//This function will print the line of asterisks
private static void printLine(int number) {
for(int i=1;i<=number;i++)
System.out.print("*");
System.out.print(" ");
}
}
__________________
Output:
Enter the number :5
*
**
***
****
*****
****
***
**
*
_____________
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.