Question

In: Computer Science

For this Lab you have to implement a class Builder. Your Builder class should have instance...

For this Lab you have to implement a class Builder. Your Builder class should have instance variable name. , Supply a constructor method for your Builder class and the following methods: getName(), makeRow(int n, String s), printPyramid(int n, String s).

Examining the problem, we need to create a Builder class, declare Builder class as follows

public class Builder {

}

Inside the Builder class, declare a String variable called name.

Step 3: Defining the constructors:

Remember that the purpose of a constructor is to assign valid values to all the data members.

public Builder (String name) {
    // write the segment of code
    // that assigns the parameter value to the private instance variable
}

Step4: Supply the methods

Implement the getName method:

Hint: getter methods often simply return the desired value, which is usually stored in a private instance variable.

public String getName() {
    // write a line of code that returns the name
}

Implement a makeRow method.

Given an integer n and string s, the makeRow method returns an other String that has n copies of s in a row. For eaxmple makeRow(5, "abc") should return "abcabcabcabcabc".

public String makeRow(int n, String s) {
    // Given an int n and string s, 
    // return a string that represents n copies of s in one row.
    //Example: n = 5, s = “*”, return a string “*****”
}

Write a method called printPyramid that given an odd integer n and a string s, prints (not returns) a pyramidal shape made out of that string s. The top of the pyramid has a single copy of s, and each successive row has two additional copies of s. The last row contains n copies of s. For example, calling printPyramid(7, "*"); prints the following lines:

   *   
  ***  
 ***** 
*******
public void printPyramid(int n, String s) {
    // Make use of makeRow method and System.out.println
    // to print the pyramid.
    // note this method does not return anything.
}

Step 5: Calling the constructor of Builder class from the “main” method.

In your Lab7.java file, you will instantiate and use a Builder class object.

import java.util.Scanner;

public class Lab7 {
  public static void main(String[] args) {
    //declare variables where you will store inputs from user 
    -->
    // declare a Scanner object 
    -->

    // prompt the user for input string name 
    -->
    // store the input in the declared variables 
    -->

    // declare a variable of type Builder named myBuilder
    // and instantiate a brand-new builder object 
    // with the name given by the user above
    -->
  }
}

Hint: Do not forget to import the Scanner package at the very top of your program.

Step 6: Calling Builder class methods and display the output

Now (also in the main method) we will call the methods in the Builder class to display the required outputs.

// call the getName() method to get the name of the builder. 
-->

// Ask for integer n from user using Scanner class
System.out.println("Enter a positive integer:");

// Using your builder's makeRow method print a string below,
// Example: =====*****===== with n = 5;
-->

// Ask for odd integer t from user using Scanner class
System.out.println("Enter a positive odd integer :");
// Call the Builder method printPyramid, passing t and "*" as the arguments
// to print pyramid with “*” as a string.
-->

Step 7: Sample Output

Below is an example of what your output should roughly look like when this lab is completed. All text in bold redrepresents user input.

First run:

Name of the builder: Alex
The name of builder is :Alex
Enter a positive integer :5

=====*****=====

Enter a positive odd integer :7

   *   
  ***  
 ***** 
*******

Second run:

Name of the builder: Bob
The name of builder is :Bob
Enter a positive integer :3

===***===

Enter a positive odd integer :11

     * 
    *** 
   ***** 
  *******
 *********
***********

Solutions

Expert Solution

Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please rate the answer. Let me know for any help with any other questions.

Thank You !
===========================================================================

public class Builder {

    private String name;
    

    public Builder(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public String makeRow(int n, String s) {

        String row = "";
        for (int i = 1; i <= n; i++) row += s;
        return row;
    }

    public void printPyramid(int n, String s) {

        for (int row = 1; row <= n; row++) {
            System.out.println(makeRow(n - row, " ") + makeRow(2 * row - 1, s));
        }

    }


}

=============================================================

import java.util.Scanner;

public class Lab7 {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter builder name: ");
        String name = scanner.nextLine();

        Builder myBuilder = new Builder(name);
        System.out.println("The name of builder is : "+myBuilder.getName());

        System.out.print("Enter a positive integer:");
        int n = scanner.nextInt();

        System.out.println();
        String row = myBuilder.makeRow(n,"=")+myBuilder.makeRow(n,"*")+
                myBuilder.makeRow(n,"=");
        System.out.println(row);
        System.out.println();
        System.out.print("Enter a positive odd integer :");
        System.out.println();
        int oddNumber = scanner.nextInt();
        myBuilder.printPyramid(oddNumber,"*");


    }
}

=============================================================


Related Solutions

For this Lab you have to implement a classBuilder. Your Builder classshould have instance...
For this Lab you have to implement a classBuilder. Your Builder class should have instance variable name. , Supply a constructor method for your Builder class and the following methods: getName(), makeRow(int n, String s), printPyramid(int n, String s).Examining the problem, we need to create aBuilder class, declare Builderclass as followspublic class Builder { }Inside the Builder class, declare aString variable called name.Step 3: Defining the constructors:Remember that the purpose of a constructor is to assign valid values to all the data members.public Builder (String name) {...
Create a class Sentence with an instance variable public Word[] words. Furthermore: The class should have...
Create a class Sentence with an instance variable public Word[] words. Furthermore: The class should have a constructor Sentence(int size), where size determines the length of the sentence field of a given Sentence. The class should have an instance method public boolean isValid() that determines the validity of a sentence according to the rules detailed below. Also create a public nested class Word, with instance variables String value and Type type. Within this class, you must create: A public enum...
(a) Create a Card class that represents a playing card. It should have an int instance...
(a) Create a Card class that represents a playing card. It should have an int instance variable named rank and a char variable named suit. Include the following methods: A constructor with two arguments for initializing the two instance variables. A copy constructor. A method equals — with one argument — which compares the calling object with another Card and returns true if and only if the corresponding ranks and suits are equal. Make sure your method will not generate...
1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class:...
1. Implement the Vehicle class.  Add the following private instance variables to the Accoun class: • An instance variable called wheelsCount of type int • An instance variable called vType of type String • An instance variable called isTruck of type boolean .  Add getters and setters for the three instance variables  Add the following methods to the Account class: • A void method called initialize that takes a parameter of type int, a String,and one double...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
#Write a class called "Burrito". A Burrito should have the #following attributes (instance variables): # #...
#Write a class called "Burrito". A Burrito should have the #following attributes (instance variables): # # - meat # - to_go # - rice # - beans # - extra_meat (default: False) # - guacamole (default: False) # - cheese (default: False) # - pico (default: False) # - corn (default: False) # #The constructor should let any of these attributes be #changed when the object is instantiated. The attributes #with a default value should be optional. Both positional #and...
ROCK, PAPER, SCISSORS Using C++, you will implement a class called Tool. It should have an...
ROCK, PAPER, SCISSORS Using C++, you will implement a class called Tool. It should have an int field called strength and a char field called type. You may make them either private or protected. The Tool class should also contain the function void setStrength(int), which sets the strength for the Tool. Create 3 more classes called Rock, Paper, and Scissors, which inherit from Tool. Each of these classes will need a default constructor that sets the strength to 1 and...
In simple Java language algorithm: Implement a static stack class of char. Your class should include...
In simple Java language algorithm: Implement a static stack class of char. Your class should include two constructors. One (no parameters) sets the size of the stack to 10. The other constructor accepts a single parameter specifying the desired size of the stack a push and pop operator an isEmpty and isFull method . Both return Booleans indicating the status of the stack Using the stack class you created in problem 1), write a static method called parse that parses...
c++ Add exception handling to the MyStack class (e.g. an instance of the class should throw...
c++ Add exception handling to the MyStack class (e.g. an instance of the class should throw an exception if an attempt is made to remove a value from an empty stack) and use the MyStack class to measure the execution cost of throwing an exception. Create an empty stack and, within a loop, repeatedly execute the following try block: try { i n t s t a c k . pop ( ) ; } catch ( e x c...
In this homework you will implement a Library class that uses your Book and Person class...
In this homework you will implement a Library class that uses your Book and Person class from homework 2, with slight modifications. The Library class will keep track of people with membership and the books that they have checked out. Book.java You will need to modify your Book.java from homework 2 in the following ways: field: dueDate (private)             A String containing the date book is due.  Dates are given in the format "DD MM YYYY", such as "01 02 2017"...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT