File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes complicated assignment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "complicated assignment" Watch "complicated assignment" New topic
Author

complicated assignment

sarah abbi
Greenhorn

Joined: Jul 02, 2005
Posts: 2
hello...would anyone be able to help me figure out this complicated assignment...i am pretty new to java...and i am studying for a midterm...this was one of the assignments we were to review...but i have no idea how to work around it...

Java program with three static methods (in addition to the main method): getValidInt, drawParallelogram, and drawTriangle. The main method will repeatedly prompt the user with a choice of: draw a parallelogram, draw a triangle, or quit. If the user asks for a parallelogram, use getValidInt to prompt for height (between 1 and 40) and for width (between 1 and 60), and call drawParallelogram, if a triangle, prompt for height (between 1 and 30), and call drawTriangle with a random choice of character from 'A' to 'Z'. If the user wants to quit, the program should terminate. Handle invalid input gracefully (i.e., don't just allow the program to crash). Please name your class a3q1.


Now some details:

Input/output in main:

The program will use JOptionPane for input (the user will enter 'p' or 'P' for a parallelogram, 't' or 'T' for a triangle, and 'q' or 'Q' to quit). If the user enters anything else, you should print "Invalid input" to System.out and prompt the user for a choice again. Output from the called methods will go to System.out, and main should print a final message identifiying the programmer and stating that the program ends normally.

getValidInt:

Input parameters include an int lowerBound, an int upperBound, and a String description containing a description of the value to enter. getValidInt will prompt the user for an integer quantity between the lower and upper bounds, using JOptionPane. The message to the user will use the description, and will state the bounds. Check the user's response against the bounds. Give the user three chances to enter a valid result. If this method receives a valid input, it will return it to the calling program. Otherwise it will return a value 1 less than the lower bound. The method should look like


public static int getValidInt(int iLowerBound, int iUpperBound, String strDescription) {
...
}



drawParallelogram:

Input parameters include ints height and width. This method will display a parallelogram made of stars of the specified size. Use System.out.print and System.out.println to draw the parallelogram.

Example: if height is 4 and width is 20, the parallelogram should look like this:

********************
********************
********************
********************
The method should look like:

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

drawTriangle:

Input parameters include the height int iHeight and the fill character char chFillCharacter. This method displays a triangle made of the filler char. Use System.out.print and System.out.println to output the triangle.

Example: if height is 5 and filler is 'A', the triangle will look like this:

AAAAA
AAAA
AAA
AA
A


The method should look like:

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

Random Letter Generation:

To generate a random character between 'A' to 'Z', use the Math.random() routine. This routine gives a number between 0.0 and (< 1.0. Here is what you do to get a random letter between 'A' and 'Z':

Declare a variable ch of type char.

Compute (int)(Math.random()*26) and store in a variable, say iTemp of type int.
Set ch = (char)('A' + iTemp). ch will now contain a random character between 'A' and 'Z'.


THANK YOU in advance...
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
The people here are not going to do the homework for you. If you need help on a specific step or if you don't understand something we will be more than glad to help.

Break the problem down into smaller pieces. Maybe start with the drawParallelogram method. Given a height and width how would you output the correct number of '*' characters (hint: sounds like you'll need loops).

See what you can do, if you get stuck on a specific spot then ask questions. Be sure to post the code you've come up with so we can help you and you can show you've been doing work.

Otherwise you can try this.
sarah abbi
Greenhorn

Joined: Jul 02, 2005
Posts: 2
i have been working on the program for awhile...we are not this advanced yet in our class, but our prof wants us to figure out these programs. this is all i have so far:

import javax.swing.*;

public class Practice
{
public static void main(String args[]) //Prompts user on whether to draw parrallelogram or triangle or quit program
{
//declare variable
String input;

//get input from user
input = JOptionPane.showInputDialog("Enter your choice of whether to:" + "\n draw a parallelogram ('p' or 'P')" + "\n draw a triangle ('t' or 'T')" + "\n quit the program ('q' or 'Q')");

//start process
if( (input.equals("p")) || (input.equals("P")) )
{
JOptionPane.showMessageDialog(null, "You chose to draw a parallelogram! Let's continue!");

}
else
if( (input.equals("t"))|| (input.equals("T")) )
{
JOptionPane.showMessageDialog(null, "You chose to draw a triangle! Let's continue!");
}
else
if( (input.equals("q")) || (input.equals("Q")) )
{
System.exit(0);
}
else
{
System.out.println(" ");
System.out.println("Invalid entry!");
System.out.println(" ");
}
System.out.println("Programmed by: Jocelyn Francis");
System.out.println(" ");
System.out.println("End of program!");
System.out.println(" ");
System.exit(0);

}//end of main method

static int getValidIntegerNumber (int upperLimit, int lowerLimit, String prompt)
throws IOException {
//This method prompts the user to input an integer within a
//given range, and validates that the input is in that
//range before returning it to the calling method
//input: upperLimit, lowerLimit, prompt
//result: number (of type int)

int number;
boolean validNumber;

do{
screen.print(prompt + " between" + lowerLimit + " and " + upperLimit + " : ");
srceen.flush();
number = new Integer(keyboard.readLine()).intValue;

if (number <= upperLimit && number >= lowerLimit){
validNumber = true;
}
else{
validNumber = false;
screen.println("Error - input outwith range.");
} //end of if
} while (!validNumber); //end of loop

return number;
} //end of method getValidIntegerNumber



/*public static int getValidInt(int iLowerBound, int iUpperBound, String strDescription)
{


}//end of getValidInt method


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

}//end ofdrawParellelogram method


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

}// end of drawTriangle method
*/

}//class
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
First this should be in Java in General(beginner), not advanced. Maybe somebody can move it there.

Looks ok so far. You need to have a while loop in the main method so they can keep going until they quit.

Do you know how to do for loops yet?

Also when you post code use code tags. There is a button just below where you type in the post.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Originally posted by Steven Bell:
First this should be in Java in General(beginner), not advanced. Maybe somebody can move it there.


Yep. Here it goes.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: complicated assignment
 
Similar Threads
Static Methods Problem
stacks
Can someone check my program?
How do I use a static method from another class?
Static Methods Problem