Sunday, February 24, 2019

Java Programing - How to get it to work?

Hi, I am studying online through uopeople and our recent assignment required us to use the class TextIO in a programming assignment. Unfortunately, the directions were lacking and despite lots of searching online, I was unable to get TextIO working and so I failed the programming assignment. One of the negatives about online learning is just that, there is no one to help you when you don't understand, despite there being a class instructor and class forum. There are tight deadlines to meet and no grace. So, I will share my attempt today to get it running in hopes I might prevent you from also failing just because it was not clear. I will be referring to whatever I can find online.

What is TextIO? http://math.hws.edu/javanotes/c2/s4.html is the link to the basic information given. To summarize, TextIO is a Java extension, new class developed by someone in order to make it easier to get text in and out of Java. Once a new class is added, it can be used as a subroutine or small program that does things you need to do in the background without your concern.

How do you get it into Netbeans? The information says: To use the TextIO class, you must make sure that the class is available to your program. What this means depends on the Java programming environment that you are using. In general, you just have to add the source code file, TextIO.java, to the same directory that contains your main program.
OK, so it sounds like all I need to do is drop it into the folder where my Java program is and then to call it in my programming. The writer then refers to 2.6 on how to use it. 2.6.4 is the Netbeans section. I got nothing from that, and then 2.6.6 The Problem of Packages - this just confused me more. Idon't understand it at this time.

Let's go to Netbeans and try to set up TextIO. My first try will be to follow a video I saw online. It is: https://www.youtube.com/watch?v=zUUeCIYEF5w I will go through it step-by-step:

1. The first step is to download the file textio.java .While finding that, I came across another post: https://www.reddit.com/r/netbeans/comments/5ekq27/trying_to_import_textiojava_into_netbeans/ and it addresses the whole package question from 2.6.6 above. it says:  
You must add the class file into your package and then import it by referencing the package. Just add it in and then at the top of any java source file you want to use you just add at the top
import package.path.ClassName; // example mypackage.TextIO
Source packages are usually located in the "src" folder.
Also do note that standard java libraries are not "part" of netbeans. They are part of the JDK (Java Development Kit) which netbeans used by default (could be JDK6, 7, 8, etc). They still need to be imported manually (ex: java.util.Scanner) What does that exactly mean that you must add the class file into your package? Is that just dropping it into the folder where my Java program is??? I hope.

Ok so here it goes.. I cleaned out my Netbeans and I will create a new Java program by File>New Project>Java>Java Application   



click Next>  that takes me to the next window where I must enter a name for the program. I will use the name mysteryio and just click Finish


Here is what I get:




There is the Source Packages folder in the program structure. Now I will try and drag a copy of TextIO.java into the source packages folder. I downloaded it from my school but you can also get it here: download
Hmmm... when I dragged and dropped it onto the folder "Source Packages", Netbeans automatically created a new folder called "<default package>". When I open it, there inside is the TextIO.java file. Here is a screen shot with the folder open:

  I think I may be getting somewhere! Now, what do I need to write in my program and where, to get it to work? Again, above it says:
You must add the class file into your package and then import it by referencing the package. Just add it in and then at the top of any java source file you want to use you just add at the top
import package.path.ClassName; // example mypackage.TextIO 
So I think it should say: import <default package>.TextIO 
Wait a minute!
I did not do anything and now I notice the change in my program.(see screenshot above)
At the top it says: package testtextio;
So, has it already been done for me? 
How can I test if it is working? 
Here is a sample program from: http://math.hws.edu/javanotes/c2/s4.html 
/**
 * A program that reads an integer that is typed in by the
 * user and computes and prints the square of that integer.
 */
 
public class PrintSquare {
    
     public static void main(String[] args) {
 
        int userInput;  // The number input by the user.
        int square;     // The userInput, multiplied by itself.
        
        System.out.print("Please type a number: ");
        userInput = TextIO.getlnInt();
        square = userInput * userInput;

        System.out.println();
        System.out.println("The number that you entered was " + userInput);
        System.out.println("The square of that number is " + square);
        System.out.println();
        
     } // end of main()
  
} //end of class PrintSquare
 
I copied and pasted this in as shown below and it failed: 
I'm stumped!

No comments:

Post a Comment