• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static Methods Problem

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, I'm trying to make a program that has four static methods (including main) that will ask if the person wants to make a parallelogram, a triangle, or quit the program (in the main method). Then its supposed to go to another method to ask for the dimensions (height and width for Parallelogram and just Height for Triangle). There is a certain range of numbers it has to be in-between (in the form of iLowerBound and iUpperBound), and the method has to close after three unsuccessful attempts. Then its either supposed to go to a parallelogram method to draw the parallelogram with those dimensions from the other method, with stars (*), or it will go to the triangle method and draw the triangle with dimensions from the previous method, with random chars.

Oh yeah, the problem has to be solved with static methods

I am having problems with the getValidInt method (Seen Below) where I can't figure out how to transfer the number range (Parallelogram height (1-40), width (1-60) and Triangle Height (1-30)) into this method. And i am not sure how to set the 3 tries and exit thing either.

Can Anyone Help Me Out?


import javax.swing.*;

public class a3q1{

public static int getValidInt(int iLowerBound, int iUpperBound, String strDescription){
String str;
int PHeight;
int PWidth;
int THeight;
int i;
boolean ThreeTry == true;

if(strDescription.equalsIgnoreCase=='P'){
str = JOptionPane.showInputDialog("Enter a Height between"+iLowerBound+"and"+iUpperBound);
PHeight = Integer.parseInt(str);

str = JOptionPane.showInputDialog("Enter a Width between"+iLowerBound+"and"+iUpperBound);
PWidth = Integer.parseInt(str);

return PHeight;
return PWidth;

if(strDescription.equalsIgnoreCase == 't'){
str = JOptionPane.showInputDialog("Enter a Height between"+iLowerBound+"and"+iUpperBound);

return THeight;
}





public static void drawParallelogram(int iHeight, int iWidth) {


}


public static void drawTriangle(int iHeight, char chFillCharacter) {

}




public static void main (String []args){
String str;
char ch;
int choice;


str = JOptionPane.showInputDialog("Enter P for Parallelogram, T for Triangle, or Q to Quit.");
ch = str.charAt(0);


if(ch.equalsIgnoreCase=='p'){
choice = getValidInt(); // for height
choice = getValidInt(); //for width
}

else if (ch.equalsIgnoreCase=='t'){
choice = getValidInt();}
else if (ch.equalsIgnoreCase=='q'){
System.exit(0);
else
System.out.println("Invalid Input, Try Again");
}
}
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Adam Favel:
... I am having problems with the getValidInt method ... where I can't figure out how to transfer the number range (Parallelogram height (1-40), width (1-60) and Triangle Height (1-30)) into this method...


You've defined this method to take 3 arguments (2 ints and a String), but when you try to call the method in main, you aren't providing any arguments. Instead of just getValidInt(), you need to use something like getValidInt(25, 35, "Square").
[ March 08, 2005: Message edited by: marc weber ]
 
Adam Favel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright...I've been working on this some more, and this is what I've come up with now...but I haven't been able to get any farther yet...can anyone help me out?

 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
zadakov/Adam Favel,

It seems as though you've just changed your display name from an acceptable one to an unacceptable one. Our
naming policy requires that you use a full, real (sounding) first and last name for
your display name. One name isn't enough (we might make an exception for Cher or Madonna if either were interested in Java.) You can change your display name back

here.
Thanks!
 
Adam Favel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there, my name is changed...but still, can anyone please look over my code, and guide me in the right direction...i'm drawing a blank.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To get this to compile, you need to provide the missing braces, declarations, initializations, and return statements. See my comments labeled "PROBLEM" in the code below (which does compile, although I might have guessed wrong on a brace or two).

But what you really need to do (when you find that the compiled code doesn't run as expected) is break this down into smaller, manageable pieces that you can test individually. Isolate each method. Write a line of code, then compile and test. If it works, then you can move on and try adding another line of code. If it doesn't work, then you know where the problem is.

HINT: In your while loop in main, do you really want to use &&?

[ March 08, 2005: Message edited by: marc weber ]
 
Adam Favel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry to bother you again, and thank you for the help, but i don't really understand what you mean by "breaking into smaller pieces". Do you mean that i should just test each static method by themselves first, and then put them back together, or am i missing the point?
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Adam Favel:
Do you mean that i should just test each static method by themselves first, and then put them back together...?


Exactly.

For example, you might write a program that just draws a triangle. When that works, you know that piece is okay.

Then you might write a problem that just asks the user for dimensions, and simply prints out a line confirming what the user entered. When that works, you know that piece is okay.

When the above two programs work separately, you might combine them: Ask the user for dimensions, then draw a triangle based on those dimensions.

Repeat this approach for any other shapes you want to draw. When you know that each of these work individually, then add code asking the user which shape they want.

In other words, confirm that each piece of your code works before you try putting it all together. This makes it much easier to find problems, and should save time (and sanity) in the long run.
 
Adam Favel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
another question...were the methods for the parallelogram and triangle actually draw the shapes they are said to draw? i'm still figuring out the problem with running it, so i currently don't know. Can someone check that out for me.

Thanks
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Adam Favel:
Sorry to bother you again, and thank you for the help, but i don't really understand what you mean by "breaking into smaller pieces". Do you mean that i should just test each static method by themselves first, and then put them back together, or am i missing the point?



Actually what breaking apart means, is that one method shouldn't have over 50 lines of code, by breaking it into smaller methods of say 10-15 lines at most, it becomes more readable. A small method will make is such that it will do only one small thing. Then methods can be called in combination to do a particular task.

Mark
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Adam Favel:
another question...were the methods for the parallelogram and triangle actually draw the shapes they are said to draw? i'm still figuring out the problem with running it, so i currently don't know. Can someone check that out for me...


Actually, that would be an ideal place for you to start.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Easy test (a good starting point)...

Nope. Nothing happens.

Can you explain line-by-line what this is supposed to do?
 
Adam Favel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

Thank you for all the help you've given me...

the triangle part of the program is supposed to print out a triangle made up of random chars created in the ch variable. it uses the height in the getValidInt() to create a triangle of that height of that random char.

Thank you again
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but what is the code really doing? This is what you need to analyze line by line. For example, focus on this part for a moment...

i is zero, and iWidth is ___. So how many times does the body of this loop execute? (In fact, how many times do we want it to execute? Should this be equal to the height or the width? Or does it matter?)

Next, look at what happens with each iteration: We print a char (with no carriage return). So even if we had the correct number of iterations, all we would get from this is a single line of chars. You need to define exactly what each iteration should do. Specifically, how many chars should be printed on the nth iteration?
 
Adam Favel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
alright, thanks for all your help, i think i can figure this out now

thank you
 
reply
    Bookmark Topic Watch Topic
  • New Topic