user2113067
user2113067

Reputation: 11

building a test program in java

Hello I am new to Java and NetBeans and am in Advanced classes in which my classes are 5 weeks long, so it is a lot to learn a new code language in 5 weeks. Anyways I have an assignment to Create a class named Movie that holds a movie name and rating. Provide methods to get and set both the movie name and rating. Create a class named TestMovie that creates three Movie instances with different values for name and rating and prints the information about each movie. I have done the code and it is passing the build fine but my professor wants a screen shot of the program working and running but I can't get NetBeans to bring that up. The chapter on building the test project was ripped out of my book. Can I get some help or pointers here is the code I have done:

package movie;

/**
 * 
 * @author Jason
 */
public class Movie {
    String movieRating;

    public Movie(String rated, String mtitle) {
        this.mrating = rated;
        this.title = mtitle;
    }

    public void setRating(String Rating) {
        movieRating = Rating;
    }

    // Get the rating
    public String getRating() {
        return movieRating;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getTitle() {
        return title;
    }

    @Override
    public String toString() {
        return "Movie" + " title=" + getTitle() + " rating=" + getRating();
    }

    public static void main(String args[]) {
        Movie mv = new Movie("", "");
        mv.toString();
    }

    private String title;
    private String mrating;
}

Upvotes: 1

Views: 9397

Answers (3)

duffy356
duffy356

Reputation: 3718

Your application prints no output, because you invoke toString(), but you don't print the result of it.

An example to create 3 Movie instances with data, print them out, and then make a screenshot of your console app.

public static void main(String args[]) {
    List<Movie> movieList = new ArrayList<Movie>(3);

    Movie mv1 = new Movie("very Good", "Testfilm 1");
    movieList add(mv1);
    mv1 = new Movie("good", "Testfilm 2");
    movieList add(mv1);
    mv1 = new Movie("not good", "Testfilm 2");
    movieList add(mv1);

    for (Movie m : movieList) {
         System.out.println(m.toString());
    }

}

Upvotes: 1

Pin
Pin

Reputation: 4016

As an alternative to the other answers suggesting printing the output to the console, with the Netbeans' UI editor you can easily create a window with a label showing the result, which makes it slightly more fancy.

You can get details on how to this here. Here's an image from that page:

enter image description here

The full working code is here. As you can see, it's just a few extra lines.

Upvotes: 1

Alxe
Alxe

Reputation: 391

You can just run a test on the console, that is, create a MovieTest class with only a main method and create three instances/objects of Movie (Movie m1, m2, m3; OR Movie[] movies;). Assign them values either in the constructor or with the set methods then print them out with the method print or println in System.out.

Something along the lines of:

public class MovieTest {
    public static void main(String[] args) {
        Movie[] movies = new Movie[] {new Movie("R1", "T1"), new Movie("R2", "T2"), new Movie("R3", "T3)";

        for (Movie i : movies) {
            System.out.println(i.toString());
        }    
    }
}

Then end by screenshooting the results.

Upvotes: 1

Related Questions