Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Java Program Add boolean method

  1. #1
    Junior Member
    Join Date
    Feb 2018
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Program Add boolean method

    I need to add a boolean method to add another movie, but want to know the steps to do so. this is my MovieTest
    import java.util.Scanner;
     
    public class MovieTest
    	{
    	public static void main(String[] args)
    	{
    	String testMovie;
    	int testRating;
    	String[] testCharacter = new String[3];
     
    	Scanner keyboard = new Scanner(System.in);
     
    	System.out.print("Enter your favorite movie: ");
    	testMovie = keyboard.nextLine();
     
    	System.out.print("Rate this movie from 1-10: ");
    	testRating = keyboard.nextInt();
     
    	keyboard.nextLine();
    	System.out.print("Who is your favorite character? ");
    	testCharacter[0] = keyboard.nextLine();
     
    	Movie movie = new Movie( testMovie, testRating, testCharacter);
     
    	System.out.println("Favorite movie: " + movie.getFavMovie());
    	System.out.println("Movie Rating: " + movie.getRating());
    	String[] character = movie.getCharacter();
    		for (int index = 0; index < character.length; index++)
    		{
    	  System.out.println("Print Favorite Character" + character[index]) ;
    		}
     
     
    	}


    }

    --- Update ---

    This is my class file
    public class Movie
    {
     //naming fields
     private String favmovie;
     private int movierating;
     private String[] character;
     
     public Movie(String movie, int rating, String[] character)
     {
      this.favmovie = movie;
      this.movierating = rating;
      this.character = character;
     
     }
     
     public void setFavMovie(String movie)
     {
      this.favmovie = movie;
     }
     
     public void setMovieRating(int rating)
     {
      this.movierating = rating;
     }
     
     public void setCharacter(String[] character)
     {
      this.character = character;
     }
     
     public String getFavMovie()
     {
      return this.favmovie;
     }
     
     public int getRating()
     {
      return this.movierating;
     }
     
     public String[] getCharacter()
     {
      return this.character;
     }
    }
    Last edited by ddaniel10; February 8th, 2018 at 08:23 AM.

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    261
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Java Program Add boolean method

    What is the condition to set it as true or false ?
    Whatever you are, be a good one

  3. #3
    Junior Member
    Join Date
    Feb 2018
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Program Add boolean method

    Implement the following method -- public boolean addMovie(String movieName, int rating, String[] characters). It should return true or false depending on the status of adding the movie.
    Implement the following method -- public boolean printCSVFile(String filename). This method should return true or false based upon the result of saving the file.
    Implement the following method -- public boolean printLabelFile(String filename). This method should return true or false based upon the result of saving the file.
    Implement the following method -- public void printMovie(int index). This method should print the movie at the given index or print an error message if you try to print an index that does not exists.
    Write a test program that enters ten movies into your system. You should then loop through your movies somewhat randomly and print out the details. Your program should display the movie details that should be printed and then print the details for the requested movie.

    Here are the instructions my teacher gave me, so im guessing to set true to continue to add a movie and false to leave it as is

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,399
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: Java Program Add boolean method

    need to add a boolean method to add another movie,
    When should the method return true and when false? What condition determines what is returned?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2018
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Program Add boolean method

    import java.util.Scanner;
     
    public class MovieTest
    	{
    	public static void main(String[] args)
    	{
    	String testMovie;
    	int testRating;
    	String[] testCharacter = new String[3];
     
    	Scanner keyboard = new Scanner(System.in);
     
    	System.out.print("Enter your favorite movie: ");
    	testMovie = keyboard.nextLine();
     
    	System.out.print("Rate this movie from 1-10: ");
    	testRating = keyboard.nextInt();
     
    	keyboard.nextLine();
    	System.out.print("Who is your favorite character? ");
    	testCharacter[0] = keyboard.nextLine();
     
    	Movie movie = new Movie( testMovie, testRating, testCharacter);
     
    	System.out.println("Favorite movie: " + movie.getFavMovie());
    	System.out.println("Movie Rating: " + movie.getRating());
    	String[] character = movie.getCharacter();
    		for (int index = 0; index < character.length; index++)
    		{
    	  System.out.println("Print Favorite Character" + character[index]) ;
    		}
     
     
    	}
     
    	public boolean addMovie(String movie,int rating, String[] character)
     
    }

    public class Movie
    {
    //naming fields
    private String favmovie;
    private int movierating;
    private String[] character;
     
    public Movie(String movie, int rating, String[] character)
    {
    this.favmovie = movie;
    this.movierating = rating;
    this.character = character;
     
    }
     
    public void setFavMovie(String movie)
    {
    this.favmovie = movie;
    }
     
    public void setMovieRating(int rating)
    {
    this.movierating = rating;
    }
     
    public void setCharacter(String[] character)
    {
    this.character = character;
    }
     
    public String getFavMovie()
    {
    return this.favmovie;
    }
     
    public int getRating()
    {
    return this.movierating;
    }
     
    public String[] getCharacter()
    {
    return this.character;
    }
    }

    im sorry im pretty new to java. The assignment was to add a boolean method ( true or false), but I don't understand why I cant just ask the user a yes to continue or no.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,399
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: Java Program Add boolean method

    why I cant just ask the user
    Can you ask your instructor about that part of the assignment?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2018
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Re: Java Program Add boolean method

    Quote Originally Posted by Norm View Post
    Can you ask your instructor about that part of the assignment?
    this is what my instructor emailed me

    "The Boolean that is returned is whether or not the movie was added. It has nothing to do with asking the user to add another. Like for example, if you are trying to add the same movie twice, you would return false since it does not make sense to have two records for the exact same movie"

    I get the part of returning false if I have two of the same movies, but I dont understand or don't know the steps to get to that.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,399
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: Java Program Add boolean method

    returning false if I have two of the same movies, but I dont understand or don't know the steps to get to that.
    Where are you storing the data about the movies? If it is in a class, does that class have a method to test if it already contains that movie?
    Use that method before adding a movie. If that movie has already been saved, return false. Otherwise add the movie and return true.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2018
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Program Add boolean method

    Quote Originally Posted by Norm View Post
    Where are you storing the data about the movies? If it is in a class, does that class have a method to test if it already contains that movie?
    Use that method before adding a movie. If that movie has already been saved, return false. Otherwise add the movie and return true.
    Yes, the class file has GetFavMovie

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,399
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: Java Program Add boolean method

    Sorry, I do not understand how your response answers the questions in my post.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java program to add two numbers
    By Prasanth.likith in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 31st, 2014, 02:33 AM
  2. My java program won't add up the numbers?
    By meaganicole in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 5th, 2012, 11:02 AM
  3. Boolean method returning a boolean value
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 09:56 AM
  4. Need help with what I believe is Boolean & add branching
    By JavaBeginner123 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: October 1st, 2010, 01:55 PM
  5. How do you add GUI to this java program?
    By leyla in forum AWT / Java Swing
    Replies: 1
    Last Post: October 18th, 2009, 01:32 PM

Tags for this Thread