Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Pascal's Triangle in Java Format


  • Please log in to reply
1 reply to this topic

#1 sidd0123

sidd0123

  • Banned
  • 81 posts
  • OFFLINE
  •  
  • Local time:08:33 AM

Posted 01 February 2010 - 06:34 PM

I have the assignment, package and class set up for my file, and I have a Main that my teacher provided so that I could test my program.

I am supposed to have 3 methods.

One should print the whole triangle at the end, one should solve the next row, and one should print the next row, which was the previous method.

I'm not good with arrays and am really confused on how to start. They only thing I could do was a getAuthor method.

My teacher wants us to do it with singular arrays.

I guess if you want to see what my teachers main looks like i can paste it here. the 10 and the 15 in the parameters are the number of lines/rows that should be printed.

Thanks

here is the main
package package04B;

public class PascalsTriMain {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

System.out.println(" Pascal's Triangle printed by " + PascalsTri.getAuthor());

PascalsTri.printTri(10);

System.out.println();

PascalsTri.printTri(15);

}

}
______________________________________…

here is my own code. not to good I know. Im not so good with arrays, actually half my class is failing cause our teachers is terrible, but i dont want to bail on the class since its IB
/**
* 
*/
package package04B;

/**
* @author *******
*
*/
public class PascalsTri {

public static String getAuthor() {
return getAuthor();
}

public static void printTri(int){
}

private static int[] nextRow(int[]){	
}

private static void printRow(int[]){
}

}
_____________________________________
I have the author method down, for next row, i am positive that I am supposed to set up an array with the first and last positions always set at 1
then i have to ask printRow method for the previous triangle (ignore that if its the first row) and add up the first two integers. then add one to the position and add the 2 and 3 and so on. then return the row to printRow method
then repeat the task until the parameters say so which is 10 and 15 as said before in my teachers main. I Know what i want to do, the problem is putting it into code.

BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:06:33 AM

Posted 02 February 2010 - 10:55 AM

I added code tags to make your code easier to read, but apparently you didn't use any indentation at all. It is important that you use indentation for readability.

Your getAuthor method simply calls the getAuthor method. It doesn't do anything, and would result in an infinite loop. You actually need a field that holds your name. Your method needs to return that field (or variable). For instance, if I want to get my dog's age, my code would look something like this:
int dogAge=4;

public int getAge(){
   return dogAge;
}

Also, as it stands, that code will not even run because you have not declared an instance of your program. Declare an instance, and then see if you can figure out how to print just the first row.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users