Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a new Java class called: NumberPyramid Write a program that will print a

ID: 3597715 • Letter: C

Question

Create a new Java class called: NumberPyramid

Write a program that will print a pyramid of integers to the screen.

The program should begin by asking the user for the number of lines to be printed. The user is expected to enter a number from 1 to 9. As long as the user does not enter a number from 1 to 9, the program should continue asking for a number from 1 to 9.

Once a correct number has been entered, the program should print a pyramid of integers as shown in the examples below.

The following is an example of what your MIGHT see on the screen when your program runs. The exact output depends on what values that the user types in while the program runs. The user's inputted values are shown below in italics:

Enter the number of lines (1-9): 7

            1
          2 1 2
        3 2 1 2 3
      4 3 2 1 2 3 4
    5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
7 6 5 4 3 2 1 2 3 4 5 6 7

Here is another example run:

Enter the number of lines (1-9): 0
Enter the number of lines (1-9): 10
Enter the number of lines (1-9): 4

      1
    2 1 2
3 2 1 2 3
4 3 2 1 2 3 4

Technical Notes & Hints:

You will need to use several loops to complete this program - some of them are nested.

Use a loop to keep asking the user to enter the number of lines, until a correct number has been entered.

Use a loop to count through the number of lines to be printed. Inside this loop, use several nested loops to:

Print the correct number of spaces at the beginning of the line

Print the numbers starting at the current line number and counting down to 1

Print the numbers from 1, and counting back up to the current line number

Explanation / Answer

import java.util.Scanner;


public class Student {


protected String name;

protected int Class;

protected char section;

protected int s1;

protected int s2;

protected int s3;

protected int s4;


public Student() {

}


public Student(String name, int Class, char section, int s1, int s2, int s3, int s4) {

this.name = name;

this.Class = Class;

this.section = section;

this.s1 = s1;

this.s2 = s2;

this.s3 = s3;

this.s4 = s4;
    }


public String getName() {

return name;
    }


public void setName(String name) {

this.name = name;
    }


public int get_Class() {

return Class;
    }


public void setClass(int Class) {

this.Class = Class;
    }


public char getSection() {

return section;
    }


public void setSection(char section) {

this.section = section;
    }


public int getS1() {

return s1;
    }


public void setS1(int s1) {

this.s1 = s1;
    }


public int getS2() {

return s2;
    }


public void setS2(int s2) {

this.s2 = s2;
    }


public int getS3() {

return s3;
    }


public void setS3(int s3) {

this.s3 = s3;
    }


public int getS4() {

return s4;
    }


public void setS4(int s4) {

this.s4 = s4;
    }


public void getData() {

Scanner in = new Scanner(System.in);

System.out.println("Enter name:");

name = in.nextLine();

System.out.println("Enter class:");

Class = in.nextInt();

System.out.println("Enter Section:");

section = in.next().charAt(0);

System.out.println("Enter marks in 4 subjects:");

s1 = in.nextInt();
        s2 = in.nextInt();
        s3 = in.nextInt();
        s4 = in.nextInt();


    }


public void showData() {

System.out.println("Name is: " + name + " Class is: " + Class + " Section is: " + section + " Marks in 4 subjects are: " + s1 + " " + s2 + " " + s3 + " " + s4);

}


public int getTotal() {

return (s1 + s2 + s3 + s4);

}


public float getPercentage() {

return (float) (getTotal() / 4);

}


public static void main(String[] args) {
Student s1 = new Student("Gourav", 12, 'A', 89, 95, 75, 81);

s1.showData();

System.out.println("Total is: " + s1.getTotal());
System.out.println("Percentage is: " + s1.getPercentage());
Student s2 = new Student();

s2.getData();
s2.showData();
System.out.println("Total is: " + s2.getTotal());

System.out.println("Percentage is: " + s2.getPercentage());

    
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote