Google Answers Logo
View Question
 
Q: Need some java help please!!! ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Need some java help please!!!
Category: Computers > Programming
Asked by: fikal-ga
List Price: $15.00
Posted: 07 Feb 2005 06:44 PST
Expires: 09 Mar 2005 06:44 PST
Question ID: 470306
I have java program due very soon for college.  The problem is I dont
even know how to start it.  I do not want the whole program done,
because you dont learn that way.  The assignment is located at :
http://www.cs.ucf.edu/courses/cop3330/spr2005//section2/Assignment2.doc
. The test program we are supposed to use the code for is located at :
http://www.cs.ucf.edu/courses/cop3330/spr2005//section2/Assignment2ATest.java

I would maybe just like a similar program that did :

public static int getInteger()
public static void leftPrint(int data, int fieldLength)

That way I can get a feel for what I need to do.

Thanks
Answer  
Subject: Re: Need some java help please!!!
Answered By: answerguru-ga on 07 Feb 2005 21:03 PST
Rated:5 out of 5 stars
 
Hi fikal-ga,

When creating a program such as the one included in your assignment,
you need a class declaration (that is called MyConsoleIO in this
case). This declaration will contain class variables, as well as
methods which can work with these variables.

Here is the sample using the two methods you specified:

public class MyConsoleIO
{
  private int anInteger = 0;

  //simple accessor method
  public static int getInteger()
  {
    return anInteger;
  }

  //leftPrint for integer input data
  public static void leftPrint(int data, int fieldLength)
  {
    int dataLength = Integer.toString(data).length();  //number of digits
    int emptySpace = 0;  //initialize

    System.out.print(data); //output data value first

    if(fieldLength - dataLength > 0) //need to pad the field
    {
      emptySpace = fieldLength - dataLength;
      
      for(int i=0; i < emptySpace; i++)
      {
        System.out.print("^");
      }
    }
  }
}


So the getInteger() method is fairly simple - it just retrieves a
single stored value. The leftPrint above does the following:

1. Takes in two integer values: the actual data value to print and a
field size value.
2. Calculates how many digits the integer will take up
3. Calculates the number of remaining spaces (that need to be padded in)
4. If there actually are spaces to be padded in, iterate through using
a loop to insert the appropriate number of spaces.

That's it! Now your job is the do the same for the remaining methods.
Remember that all of the printing methods are overloaded, meaning that
the same method name is being reused with different input data types.

Hope that helps :)

Cheers!

answerguru-ga

Request for Answer Clarification by fikal-ga on 07 Feb 2005 22:07 PST
where do i put public class MyConsoleIO { etc ?  Does this go in the
same program as the sample...or where do i need to keep this file?

Clarification of Answer by answerguru-ga on 08 Feb 2005 00:41 PST
It should reside in a file called MyConsoleIO.java which is in the
same directory as your test file.

Thank you for using Google Answers :)

answerguru-ga

Request for Answer Clarification by fikal-ga on 13 Feb 2005 09:11 PST
Hmm I finished the code but now i have some errors with the 

public static int getInteger()
  {
    return anInteger;
  }

MyConsoleIO.java:10: non-static variable myInteger cannot be
referenced from a static context
		return myInteger;

Says that for each of my get statements.

http://www15.brinkster.com/fikal/MyConsoleIO.java
http://www15.brinkster.com/fikal/Assignment2ATest.java

Clarification of Answer by answerguru-ga on 13 Feb 2005 09:35 PST
Try changing this line:

private int anInteger = 0;

to this:

private static int anInteger = 0;

Request for Answer Clarification by fikal-ga on 13 Feb 2005 10:51 PST
Well now were on the right track!  The program actually ran, but I
think we still may be missing something from the getInterger.  When I
run the assignment2Atest.java, I dont have an option to input numbers.
 This was the run output!

Enter an integer number:^^^^^^
|0^^^^^^^^^|^^^^0^^^^|^^^^^^^^^0|

Enter a double number:^^^^^^^^
|0.0^^^^^^^|^^^^0.0^^^^|^^^^^^^0.0|

Enter a character string:^^^^^
|java.lang.NullPointerException
	at MyConsoleIO.leftPrint(MyConsoleIO.java:138)
	at Assignment2ATest.main(Assignment2ATest.java:58)

Clarification of Answer by answerguru-ga on 13 Feb 2005 11:17 PST
The MyConsoleIO class isn't meant to be taking input - that is what
the main() method of the Assignment2ATest.java is supposed to do. That
is beyond the scope of this question.

If you would like to continue this in another question, myself or
another researcher would be happy to help.

Thanks for using Google Answers!

Request for Answer Clarification by fikal-ga on 13 Feb 2005 12:42 PST
So i have to be adding something to the Test.java file?  I was given
the impression from the professor that the Test.java file was supposed
to be unaltered.

I understand about adding something like this inside the test.jave file:

inputString = stdin.readLine();
int newInteger = Integer.parseInteger(inputString);

But I think he made the test file to be able to fully run w/o being edited.

Request for Answer Clarification by fikal-ga on 13 Feb 2005 17:27 PST
Actually im possitive that the Assignment2ATest.java file is not
supposed to be updated.  So the MyConsoleIO does need to include code
to prompt for the integer,etc.  I believe this is within the scope of
my question since i asked for the code of leftPrint.

Clarification of Answer by answerguru-ga on 13 Feb 2005 19:02 PST
Hi again,

I took a closer look at this and what is missing from your program is
outside the scope of what you asked for. You asked for the following
method:

leftPrint(int data, int fieldLength)

From looking at your test program, it is clear that it is expecting
another method which is used for obtaining user input. The signature
for this method should look like this:

leftPrint(String message, int fieldLength)

The method above would display the message provided and wait for the
user to enter the correct information. Please create a new question if
you would like additional assistance with this problem.

Request for Answer Clarification by fikal-ga on 14 Feb 2005 10:58 PST
If you looked closely, you would have seen that I wanted help with the 
getInteger() function also and not just the leftPrint function.

As stated in the bottom of my original question :

"
I would maybe just like a similar program that did :

public static int getInteger()
public static void leftPrint(int data, int fieldLength) "


I was luckly able to solve the program w/o your help though.  The
getInteger code that you gave me needed more code which I guess I
didnt make clear somehow.

If you are curious of how it was supposed to look :

import java.io.*;

public class MyConsoleIO 
{ 
	private static int myInteger = 0; 
	private static double myDouble = 0;
	private static String myString = "";
	private static String inputString = "";	
			
	//simple accessor method 
	
	public static int getInteger() throws java.io.IOException 
	{
		String inputString;// Used to temporarily hold console input
		BufferedReader stdin = new BufferedReader(new
InputStreamReader(System.in)); //Read user input and convert to
integer
		inputString = stdin.readLine();
		myInteger = Integer.parseInt(inputString);
		return (myInteger);
	}  // End of getInteger() method

Please try to read over questions more closely next time.
fikal-ga rated this answer:5 out of 5 stars

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy