Question

Project 12, page 312 in DATA STRUCTURES and ABSTRACTIONS with JAVA BY FRANK M. CARRANO AND...

Project 12, page 312 in DATA STRUCTURES and ABSTRACTIONS with JAVA BY FRANK M. CARRANO AND TIMOTHY M. HENRY

You will create two files: PascalTriangle.java and Driver.java. The Driver file will contain the main

method. The main method will ask the user what size triangle to create, that is to say how many rows

the triangle will have. It will then create an instance of the pascalTriangle class, passing the constructor

the number of rows. It will then loop to printout all the rows. If a size of 10 was entered it should

printout the following:

The Pascal Triangle of size 10 is

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

1 6 15 20 15 6 1

1 7 21 35 35 21 7 1

1 8 28 56 70 56 28 8 1

1 9 36 84 126 126 84 36 9 1

Be sure to follow the instructions for the PascalTriangle class in the book

0 0
Add a comment Improve this question Transcribed image text
Answer #1

input code:

driver.java

1 import java.util.*; 2 /*make Driver class which extends Pascal Triangle class*/ 3 public class Driver extends PascalTriangl

PascalTriangle.java

1 public class PascalTriangle { public static int n; /*make constructor*/ public PascalTriangle() Soovau AWN this.n=0; public

output:

11 1 21 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9

code:

Driver.java

import java.util.*;
/*make Driver class which extends PascalTriangle class*/
public class Driver extends PascalTriangle
{
public static void main(String[] args)
{
/*create a Object*/
PascalTriangle p1=new PascalTriangle(10);
/*call the method*/
p1.Triangle();
}
}

PascalTriangle.java

public class PascalTriangle
{
public static int n;
/*make constructor*/
public PascalTriangle()
{
this.n=0;
}
public PascalTriangle(int m)
{
this.n=m;
}
/*make method for print the PascalTriangle*/
static void Triangle()
{
for (int j = 0; j < n; j++)
{
/*print the value in line*/
for (int i = 0; i <= j; i++)
{
/*call value function and print the value*/
System.out.print(value(j, i)+" ");
}
/*print the line*/
System.out.println();
}
}
  
/*make function for generate the value*/
public static int value(int m, int k)
{
int ans = 1;
/*check the condition*/
if (k > m - k)
k = m - k;
/*calculate the value*/
for (int i = 0; i < k; ++i)
{
ans *= (m - i);
ans /= (i + 1);
}
/*return value*/
return ans;
}
}

Add a comment
Know the answer?
Add Answer to:
Project 12, page 312 in DATA STRUCTURES and ABSTRACTIONS with JAVA BY FRANK M. CARRANO AND...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Note: The assignment must be submitted into the designated link in the Blackboard cou page. You...

    Note: The assignment must be submitted into the designated link in the Blackboard cou page. You are to save each HTML5 document file as the following synt StudentID FirstName_LastName Question Number ► QUESTIONS In this assignment you are required to write HTML5 code which contains JavaScript in order to do the following tasks: 1) Construct the below multiplication table using JavaScript code and HTML5 1 2 3 4 5 6 7 8 9 10 11 12 111 2 3 4...

  • Create a class called Play that has an InputReader as an instance variable. Be sure to...

    Create a class called Play that has an InputReader as an instance variable. Be sure to initialize it in the constructor. The class has two methods. Write a method with this signature: Write a method with this signature: public void stringPlay() The method prompts the user for a string, reads it in, and then displays the string as many times as the length of that string. The output string should be formatted with the first letter uppercase and the rest...

  • Java Project Create a NetBeans project for this activity. Test Stem / Question Choices 1: Create...

    Java Project Create a NetBeans project for this activity. Test Stem / Question Choices 1: Create a new class with attributes name and address. Both data type will be String. Create a constructor and assign the parameters to the attributes. Now override the toString() method. Is this possible? A: Yes. B: No. C: In some cases where the attributes are only strings. D: Yes, as long as you explicitly extends Object class. 2: Create a class with the main method...

  • signature 1. Create a new NetBeans Java project. The name of the project has to be...

    signature 1. Create a new NetBeans Java project. The name of the project has to be the first part of the name you write on the test sheet. The name of the package has to be testo You can chose a name for the Main class. (2p) 2. Create a new class named Address in the test Two package. This class has the following attributes: city: String-the name of the city zip: int - the ZIP code of the city...

  • This program is C++ You have just been offered your first big software contract. The scope...

    This program is C++ You have just been offered your first big software contract. The scope of work is to create a tool for grade school students to learn their times tables. In this program, you will create a 10 x 10 grid of times tables as a cheat sheet for the students to learn. This will display the values of the times tables all the way from 1x1 to 10x10. Make use of formatting tools like inserting tabs, setwidth(),...

  • Need help with this Java project implementing an interpolation search, bolded the missing parts: /**   ...

    Need help with this Java project implementing an interpolation search, bolded the missing parts: /**    A class that implements an interpolation search and    a binary search of a sorted array of integers.    @author Frank M. Carrano    @author Timothy M. Henry    @version 4.0 */ public class SearchComparer {    private int[] a;    private int interpolationSearchCounter;    private int binarySearchCounter;    private static final int CAPACITY = 50;    public SearchComparer(int[] array, int n)    {...

  • JAVA (advanced data structures) write a completed program using HashIntSet and HashMain include the method in the main...

    JAVA (advanced data structures) write a completed program using HashIntSet and HashMain include the method in the main if possible. those are just the sample HashIntSet and HashMain (don't have to be the same but follow the Q requirement. thanks HashIntSet.java public class HashIntSet { private static final double MAX_LOAD_FACTOR = 0.75; private HashEntry[] elementData; private int size; // Constructs an empty set. public HashIntSet() { elementData = new HashEntry[10]; size = 0; } // Adds the given element to...

  • Write a Java program, In this project, you are going to build a max-heap using array...

    Write a Java program, In this project, you are going to build a max-heap using array representation. In particular, your program should: • Implement two methods of building a max-heap. o Using sequential insertions (its time complexity: ?(?????), by successively applying the regular add method). o Using the optimal method (its time complexity: ?(?), the “smart” way we learned in class). For both methods, your implementations need to keep track of how many swaps (swapping parent and child) are required...

  • Suppose a binary tree data (in tiny written size) is stored in an array (A) as...

    Suppose a binary tree data (in tiny written size) is stored in an array (A) as given below and root is placed at “0”index. Note the array indices are in larger written size (0 to 74). Show the traversal data of the given tree for a)      In-Order Traversal b)     Post Order Traversal A 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 3 28 13 36 15 9 22 44 7 10 75 33 19 15...

  • Java

    Task #1 Creating a New Class1. In a new file, create a class definition called Television.2. Put a program header (comments/documentation) at the top of the file// The purpose of this class is to model a television// Your name and today's date3. Declare the 2 constant fields listed in the UML diagram.4. Declare the 3 remaining fields listed in the UML diagram.5. Write a comment for each field indicating what it represents.6. Save this file as Television.java.7. Compile and debug....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT