need help on an assignment using methods.
The assignment I'm working on uses methods to input an integer and a character and then output a hollow square of the size of the integer and made up of the character entered. This is what I have so far, I'm not quite sure how to fill in the methods....
import java.util.Scanner;//For scanner class
publicclass Methods
{publicstaticvoid giveInstructions()
//instructions
{System.out.println("Enter a character and an integer. A hollow square of size n times n made up of the character" +
" will be constructed.");
}
publicstaticint getSize (Scanner kbd)
{
int size;
Scanner kbd =new Scanner (System.in);
System.out.println("Enter the size: ");
size = kbd.nextInt();
}
publicstaticchar getCharacter (Scanner kbd)
{
System.out.println("Enter the Character: ");
finalchar ch;
ch = kbd.nextLine();
}
publicstaticvoid drawSquare(int size,char ch)
{
}
publicstaticvoid drawSolidLine(int size,char ch )
{
String s;
for(int i = 0; i < size ; i++ )
{
// ADD ch
}
System.out.println( s );
}
publicstaticvoid drawHollowLine(int size,char ch )
{
String s ="";
// add char to s
for(int i = 0 ; i < ( size - 2) ; i++ )
{
s = s +" ";
}
// Add char to s
System.out.println( s );
}
}
I would appreciate any kind of help!
[3443 byte] By [
inhereyesa] at [2007-10-2 7:32:44]

Here's a tip: You need to add a main method that calls the other methods to make your program work. The main method is what gets called when you run your Java program. It's signature has to look like this:
public static void main(String[] args) {
// ... add some code here to call the methods you wrote
}
Another tip: work one method at a time. Does your first method compile? If it does, you could even call it from the main() method to see if it works.
Then the second method. Fails to compile? So take the first compilation error, read the error message (REALLY read it), think about it, then fix it. Repeat until all done.
how do you call the methods?
> how do you call the methods?"Hey you."You call them by their name.Methods.drawSquare(17342789, 'A'); // static methods are called wth class reference
So under the main method, what would i write to call the other methods?
> So under the main method, what would i write to call> the other methods? http://java.sun.com/docs/books/tutorial/ Have a blast.
> So under the main method, what would i write to call
> the other methods?
That's up to you.
Take a look at this:
public class Test {
// constructor
public Test() {}
public void printA() {
System.out.println("A");
}
public void printB() {
System.out.println("B");
}
public void printSomething(String s) {
System.out.println(s);
}
public static void main(String[] args) {
Test t = new Test();
t.printA();
t.printB();
t.printSomething("AB");
}
}
Good luck.
I'm really confused. I can't think of anything that I would need to write under the main method because everything that I need would be under the other methods.
> I'm really confused. I can't think of anything that I
> would need to write under the main method because
> everything that I need would be under the other
> methods.
I assume you're replying to me(?). Did you compile and run my example? What is it you don't understand about it? I see your methods are all static so you could do something like this:
public class Test {
public static void printA() {
System.out.println("A");
}
public static void printB() {
System.out.println("B");
}
public static void printSomething(String s) {
System.out.println(s);
}
public static void main(String[] args) {
Test.printA();
Test.printB();
Test.printSomething("AB");
}
}
Ok here are some things I don't understand....
I'm not sure how to get the character ch to become what is entered. I tried writing this, but i keep getting errors...
public static char getCharacter (Scanner kbd)
{
final char ch;
System.out.println("Enter the Character: ");
return ch;
}
It says that the variable ch wasn't initialized.
Also, if I need to include a main method....are the other methods within the main method?
@OP: please, please, please get a personal tutor.
> public static char getCharacter (Scanner kbd)
> {
>final char ch;
> System.out.println("Enter the Character: ");
> return ch;
> }
>
> It says that the variable ch wasn't initialized.
>
> Also, if I need to include a main method....are the
> other methods within the main method?
Well, did you put a Scanner object in the method? After you do that, you'll need to read a char from the user with the Scanner object and return that char:
public static char getCharacter (Scanner kbd)
{
final char ch;
System.out.print("Enter the Character: ");
ch = // something with the Scanner object here.
return ch;
}
@OP: please, please, please get a personal tutor.
I tried that, I wrote....
public static char getCharacter (Scanner kbd)
{
final char ch;
System.out.println("Enter the Character: ");
ch = kbd.nextLine();
return ch;
}
but it comes up with an error saying incompatible types
> @OP: please, please, please get a personal tutor.Seconded.
I dont have time to get a tutor. My assignment is due this week. It' s the last program I will ever have to write.
> I dont have time to get a tutor. My assignment is due> this week. It' s the last program I will ever have to> write.How about asking a peer? Sounds like you're taking a class, you're certainly not the only person taking it.
> I dont have time to get a tutor. My assignment is due> this week. It' s the last program I will ever have to> write.Then maybe you should take a look at my previous post.