Question

In: Computer Science

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 follows

public 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) {
    // 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 strings, the makeRow method returns an other String that has n copies ofs 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 strings. The top of the pyramid has a single copy ofs, and each successive row has two additional copies of s. The last row containsn 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 Scannerpackage 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: AlexThe name of builder is :Alex
Enter a positive integer :5=====*****=====

Enter a positive odd integer :7

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

Second run:

Name of the builder: BobThe name of builder is :Bob
Enter a positive integer :3===***===

Enter a positive odd integer :11 * 
    *** 
   ***** 
  *******
 *********
***********

Solutions

Expert Solution

// Builder.java

public class Builder {
private String name;
public Builder(String name) {
// write the segment of code
// that assigns the parameter value to the private instance variable
this.name = name;
}
public String getName() {
// write a line of code that returns the name
return name;
}
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 “*****”
String str = "";
for (int i = 0; i < n; i++) {
str += "=";
}
for (int i = 0; i < n; i++) {
str += s;
}
for (int i = 0; i < n; i++) {
str += "=";
}
return str;
}
public void printPyramid(int n, String s) {
int size =n; //use only odd numbers here
for (int i = 1; i <= size; i=i+2) {
int spaceCount = (size - i)/2;
for(int j = 0; j< size; j++) {
if(j < spaceCount || j >= (size - spaceCount)) {
System.out.print(" ");
} else {
System.out.print(s+" ");
}
}
System.out.println();
}
}
}
_______________________________

// Lab10.java

import java.util.Scanner;
public class Lab10 {
public static void main(String[] args) {
//declare variables where you will store inputs from user
String s;
int n;
// declare a Scanner object
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
// prompt the user for input string name
System.out.print("Enter String :");
// store the input in the declared variables
s=sc.next();
// declare a variable of type Builder named myBuilder
// and instantiate a brand-new builder object
// with the name given by the user above
Builder b=new Builder(s);
  
// call the getName() method to get the name of the builder.
System.out.println("The name of builder is :"+b.getName());
// Ask for integer n from user using Scanner class
System.out.print("Enter a positive integer:");
// Using your builder's makeRow method print a string below,
// Example: =====*****===== with n = 5;
n=sc.nextInt();
System.out.println(b.makeRow(n, "*"));
// Ask for odd integer t from user using Scanner class
System.out.println("Enter a positive odd integer ,:");
System.out.print("representing number of stars in the base f a pyramid:");
  
// Call the Builder method printPyramid, passing t and "*" as the arguments
// to print pyramid with “*” as a string.
n=sc.nextInt();
b.printPyramid(n,"*");
}
}

__________________________________

Output#1:

Enter String :Alex
The name of builder is :Alex
Enter a positive integer:5
=====*****=====
Enter a positive odd integer ,:
representing number of stars in the base f a pyramid:7
*   
* * *   
* * * * *   
* * * * * * *

________________________

Output#2:

Enter String :Bob
The name of builder is :Bob
Enter a positive integer:9
=========*********=========
Enter a positive odd integer ,:
representing number of stars in the base f a pyramid:21
*   
* * *   
* * * * *   
* * * * * * *   
* * * * * * * * *   
* * * * * * * * * * *   
* * * * * * * * * * * * *   
* * * * * * * * * * * * * * *   
* * * * * * * * * * * * * * * * *   
* * * * * * * * * * * * * * * * * * *   
* * * * * * * * * * * * * * * * * * * * *


Related Solutions

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 java code In this lab you will implement an inorder traversal, and a search for...
a java code In this lab you will implement an inorder traversal, and a search for a 2-3-4 tree. The inorder traversal will print the contents (integers) of the tree, and the search method will return a boolean, indicating whether a given key was found in the tree. Examine the provided template. The main method is provided for you, as well as a template of the Node and 2-3-4 classes. The main method will read either "inorder" or an integer...
In this lab, you will implement and evaluate the performance (run time and # of conflicts),...
In this lab, you will implement and evaluate the performance (run time and # of conflicts), for different load factors, of three hashing schemes: • linear, • quadratic, and • double hashing. The required files for this lab are Driver.java, DoubleHash.java, KeyValuePair.java, LinearProbe.java, PrimeNumber.java, QuadraticProbe.java, and country-capitals.xls. You are required to submit a short report comparing, using plots, the performance of the hashing schemes for adding and retrieving items for different load factors (try at least 0.25, 0.5, 0.65). /**...
7.14 LAB: Temperature conversion In this lab, you will implement a temperature converter. Five UI elements...
7.14 LAB: Temperature conversion In this lab, you will implement a temperature converter. Five UI elements are declared for you in the template: Element's ID Element description cInput Text input field for Celsius temperature fInput Text input field for Fahrenheit temperature convertButton Button that, when clicked, converts from one temperature to the other errorMessage Div for displaying an error message when temperature cannot be converted weatherImage Image corresponding to the temperature Implement the conversion functions (2 points) Implement the convertCtoF()...
describe a specific instance where you have provided support according to your duty of care as...
describe a specific instance where you have provided support according to your duty of care as a care worker
Lab Scenario: You have a 1 W light bulb in your lab. It puts out light...
Lab Scenario: You have a 1 W light bulb in your lab. It puts out light of only 1 frequency. The wavelength of this light is 500nm. you set up a detector with a surface area of 1 square centimeter facing the light source at a distance of 100m a) Find intensity of light due to your 1 W light bulb at the location of the detector. Answer in micro W/m^2. b) Find the number of photons hitting the detector...
In this lab, you will implement Heap Sort algorithm in C++ and Report the number of...
In this lab, you will implement Heap Sort algorithm in C++ and Report the number of steps and the CPU running time in a table, please show the code and output Approximation the constant c in the complexity of heap sort (cnlgn) by inspecting the results For each algorithm, and for each n = 100, 200, 300, 400, 500, 1000, 4000, 10000, measure its running time and number of steps when the input is (1) already sort, i.e. n, n-1,...
You are a field researcher for the CDC. You and your lab partner have been sent...
You are a field researcher for the CDC. You and your lab partner have been sent to an area to quantitatively analyze a set of samples believed to contain pathological bacterial contaminants. On a separate sheet of paper, write (NEATLY) or type the procedure you used to quantitatively analyze the given bacterial suspension. Include steps in the procedure and materials used (5.5 pts). Be organized, logical, and neat in documentation of this information. Write your results. Show your work. No...
1. Describe a specific instance where you have provided support according to your duty of care...
1. Describe a specific instance where you have provided support according to your duty of care as a care worker? 2.Describe a specific instance where you have provided support according to the dignity of risk requirements? 3. Describe how you assited in maintaining a safe and healthy enviroment in the care centre? Describe how you assited in maintaining a clean and comfortable enviroment in the care centre? 4. Describe how you ensured your clients maximum dignity and privacy? 5.Describe how...
In this lab, you will implement Heap Sort algorithm for the same inputs. For each algorithm,...
In this lab, you will implement Heap Sort algorithm for the same inputs. For each algorithm, and for each n = 100, 200, 300, 400, 500, 1000, 4000, 10000, measure its running time and number of steps when the input is (1) already sort, i.e. n, n-1, …, 3, 2,1; (2) reversely sorted 1, 2, 3, … n; (3) random permutation of 1, 2, …, n; (4) 50 instances of n random numbers generated in the range of [1..n]. Note:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT