Question

import java.util.*;
public class Movie {
  
private String movieName;
private int numMinutes;
private boolean isKidFriendly;
private int numCastMembers;
private String[] castMembers;
  
// default constructor
public Movie()
{
movieName = "Flick";
numMinutes = 0;
isKidFriendly = false;
numCastMembers = 0;
castMembers = new String[10];
}
  
// overloaded parameterized constructor
public Movie(String movieName, int numMinutes, boolean isKidFriendly, String[] castMembers)
{
this.movieName = movieName;
this.numMinutes = numMinutes;
this.isKidFriendly = isKidFriendly;
numCastMembers = castMembers.length;
this.castMembers = new String[numCastMembers];
  
for(int i=0;i<castMembers.length;i++)
{
this.castMembers[i] = castMembers[i];
}
}
  
// set the number of minutes
public void setNumMinutes(int numMinutes)
{
this.numMinutes = numMinutes;
}
  
// set the movie name
public void setMovieName(String movieName)
{
this.movieName = movieName;
}
  
// set if the movie is kid friendly or not
public void setIsKidFriendly(boolean isKidFriendly)
{
this.isKidFriendly = isKidFriendly;
}
  
// return the movie name
public String getMovieName()
{
return movieName;
}
  
// return the number of minutes
public int getNumMinutes()
{
return numMinutes;
}
  
// return true if movie is kid friendly else false
public boolean isKidFriendly()
{
return isKidFriendly;
}
  
// return the array of cast members
public String[] getCastMembers()
{
// create a deep copy of the array
String [] copyCastMembers = new String[castMembers.length];
// copy the strings from the array to the copy
for(int i=0;i<castMembers.length;i++)
{
copyCastMembers[i] = castMembers[i];
}
  
return copyCastMembers;
}
  
// return the number of cast members
public int getNumCastMembers()
{
return numCastMembers;
}
  
// method that allows the name of a castMember at an index in the castMembers array to be changed
public boolean replaceCastMember(int index, String castMemberName)
{
// validate the index
if(index >= 0 && index < castMembers.length)
{
// if valid, update the entry to castMemberName
castMembers[index] = castMemberName;
return true;
}
  
return false;
}
  
// method that determines the equality of two String arrays and returns a boolean, by comparing the value at each index location.
// Return true if all elements of both arrays match, return false if there is any mismatch.
public boolean doArraysMatch(String [] arr1, String [] arr2)
{
// both arrays are null
if(arr1 == null && arr2 == null)
return true;
else if(arr1 == null || arr2 == null) // one of the array is null
return false;
else if(arr1.length != arr2.length) // length of arrays do not match
return false;
  
// length of both arrays are same
// loop over the arrays
for(int i=0;i<arr1.length;i++)
{
// if ith element are not equal, return false
if(!arr1[i].equalsIgnoreCase(arr2[i]))
return false;
}
  
return true; // length of arrays are same and each element at respective location are same
  
}

// method that gets the contents of the array of castMembers as a comma separated String.
public String getCastMemberNamesAsString()
{
String castMembersString = "";
// loop over the array castMembers, appending the castMember's name to the string with a trailing comma
for(int i=0;i<castMembers.length;i++)
{
if(castMembers != null)
castMembersString += castMembers[i]+", ";
}
  
if(castMembersString.length() > 0)
// remove the last comma by taking the subtring from the start to the index before last index of "," in the string
castMembersString = castMembersString.substring(0,castMembersString.lastIndexOf(","));
else // no cast members
castMembersString = "none";
  
return castMembersString;
}
  
// return String representation of the movie
public String toString()
{
String movie = "Movie: [ Minutes: "+numMinutes+" | Movie Name: "+movieName+" | ";
if(isKidFriendly)
movie += "kid friendly | ";
else
movie += "not kid friendly | ";
movie += "Number of Cast Members: "+numCastMembers+" | Cast Members: "+getCastMemberNamesAsString()+" ] ";
  
return movie;
}
  
// returns true if this movie is equal to o
public boolean equals(Object o)
{
// check if o is an instance of Movie
if(o instanceof Movie)
{
Movie other = (Movie)o;
// returns true if all the fields are equal
return((movieName.equalsIgnoreCase(other.movieName)) && (isKidFriendly == other.isKidFriendly) && (numMinutes == other.numMinutes)
&& (numCastMembers == other.numCastMembers) && (doArraysMatch(castMembers,other.castMembers)));
}
  
return false; // movies are not equal or o is not an object of Movie
}
}Total score: 29/54 Latest submission - 3:29 PM on 05/19/20 Only show failing tests Download this submission 1: Test doArraysMmovieName set correctly to Never Enc OK --- numMinutes set correctly to 321 OK --- isKidFriendly set correctly to true OK ---

0 0
Add a comment Improve this question Transcribed image text
Answer #1

i made some tiny modifications here and there, except for the overloaded constructor. There is no way it is incorrect.

# If you have a query/issue with respect to the answer, please drop a comment. I will surely try to address your query ASAP and resolve the issue

//---------------------------------------------------------------------------------------

import java.util.*;

public class Movie {

    private String movieName;

    private int numMinutes;

    private boolean isKidFriendly;

    private int numCastMembers;

    private String[] castMembers;

    // default constructor

    public Movie() {

        movieName = "Flick";

        numMinutes = 0;

        isKidFriendly = false;

        numCastMembers = 0;

        castMembers = new String[10];

    }

    // overloaded parameterized constructor

    public Movie(String movieName, int numMinutes, boolean isKidFriendly, String[] castMembers) {

        this.movieName = movieName;

        this.numMinutes = numMinutes;

        this.isKidFriendly = isKidFriendly;

        numCastMembers = castMembers.length;

        this.castMembers = new String[numCastMembers];

        for (int i = 0; i < castMembers.length; i++) {

            this.castMembers[i] = castMembers[i];

        }

    }

    // set the number of minutes

    public void setNumMinutes(int numMinutes) {

        this.numMinutes = numMinutes;

    }

    // set the movie name

    public void setMovieName(String movieName) {

        this.movieName = movieName;

    }

    // set if the movie is kid friendly or not

    public void setIsKidFriendly(boolean isKidFriendly) {

        this.isKidFriendly = isKidFriendly;

    }

    // return the movie name

    public String getMovieName() {

        return movieName;

    }

    // return the number of minutes

    public int getNumMinutes() {

        return numMinutes;

    }

    // return true if movie is kid friendly else false

    public boolean isKidFriendly() {

        return isKidFriendly;

    }

    // return the array of cast members

    public String[] getCastMembers() {

        // create a deep copy of the array

        String[] copyCastMembers = new String[castMembers.length];

        // copy the strings from the array to the copy

        for (int i = 0; i < castMembers.length; i++) {

            copyCastMembers[i] = castMembers[i];

        }

        return copyCastMembers;

    }

    // return the number of cast members

    public int getNumCastMembers() {

        return numCastMembers;

    }

    // method that allows the name of a castMember at an index in the castMembers

    // array to be changed

    public boolean replaceCastMember(int index, String castMemberName) {

        // validate the index

        if (index >= 0 && index < numCastMembers) {

            // if valid, update the entry to castMemberName

            castMembers[index] = castMemberName;

            return true;

        }

        return false;

    }

    // method that determines the equality of two String arrays and returns a

    // boolean, by comparing the value at each index location.

    // Return true if all elements of both arrays match, return false if there is

    // any mismatch.

    public boolean doArraysMatch(String[] arr1, String[] arr2) {

        // both arrays are null

        if (arr1 == null && arr2 == null)

            return true;

        else if (arr1 == null || arr2 == null) // one of the array is null

            return false;

        else if (arr1.length != arr2.length) // length of arrays do not match

            return false;

        // length of both arrays are same

        // loop over the arrays

        for (int i = 0; i < arr1.length; i++) {

            // if ith element are not equal, return false

            if (!arr1[i].equalsIgnoreCase(arr2[i]))

                return false;

        }

        return true; // length of arrays are same and each element at respective location are same

    }

    // method that gets the contents of the array of castMembers as a comma

    // separated String.

    public String getCastMemberNamesAsString() {

        if (numCastMembers == 0) {

            return "none";

        }

        String names = castMembers[0];

        for (int i = 1; i < numCastMembers; i++) {

            names += ", " + castMembers[i];

        }

        return names;

    }

    // return String representation of the movie

    public String toString() {

        String movie = "Movie: [ Minutes %03d | Movie Name: %15s | ";

        if (isKidFriendly)

            movie += "is kid friendly | ";

        else

            movie += "not kid friendly | ";

        movie += "Number of Cast Members: " + numCastMembers + " | Cast Members: " + getCastMemberNamesAsString()

                + " ] ";

        return String.format(movie,numMinutes,movieName);

    }

    // returns true if this movie is equal to o

    public boolean equals(Object o) {

        // check if o is an instance of Movie

        if (o instanceof Movie) {

            Movie other = (Movie) o;

            // returns true if all the fields are equal

            return ((movieName.equalsIgnoreCase(other.movieName)) && (isKidFriendly == other.isKidFriendly)

                    && (numMinutes == other.numMinutes) && (numCastMembers == other.numCastMembers)

                    && (doArraysMatch(castMembers, other.castMembers)));

        }

        return false; // movies are not equal or o is not an object of Movie

    }

}

//-------------------------------------------------------------------------------------------

Add a comment
Answer #2

aaaaaaaaaaaaaa 3

Add a comment
Know the answer?
Add Answer to:
import java.util.*; public class Movie {    private String movieName; private int numMinutes; private boolean isKidFriendly;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • \\ this is the code i need help to get this part working the rest works...

    \\ this is the code i need help to get this part working the rest works but this and im not sure what is missing or how to make it work. this is what they asking for to do and there is two errors the ones shown in the pictures this is all the information I have from zybooks\\ Q3. (54 Points) Complete a public class to represent a Movie as described below. (a-e 4 pts each) (f-h 8 pts...

  • public class Fish { private String species; private int size; private boolean hungry; public Fish() {...

    public class Fish { private String species; private int size; private boolean hungry; public Fish() { } public Fish(String species, int size) { this.species = species; this.size = size; } public String getSpecies() { return species; } public int getSize() { return size; } public boolean isHungry() { return hungry; } public void setHungry(boolean hungry) { this.hungry = hungry; } public String toString() { return "A "+(hungry?"hungry":"full")+" "+size+"cm "+species; } }Define a class called Lake that defines the following private...

  • Consider the class as partially defined here: //class Pizzaorder class Pizzaorder // static public members public...

    Consider the class as partially defined here: //class Pizzaorder class Pizzaorder // static public members public static final int MAX TOPPINGS 20: // instance members private String toppings[]; private int numToppings; // constructor public PizzaOrder () { numToppings toppings 0; String[MAX TOPPINGS]; new // accessor tells # toppings on pizza, i.e., #toppings in array public int getNum Toppings ()return numToppings; // etc. We want to write an instance method, addTopping) that will take a String parameter, topping, and add it...

  • Consider the Automobile class: public class Automobile {    private String model;    private int rating;...

    Consider the Automobile class: public class Automobile {    private String model;    private int rating; // a number 1, 2, 3, 4, 5    private boolean isTruck;    public Automobile(String model, boolean isTruck) {       this.model = model;       this.rating = 0; // unrated       this.isTruck = isTruck;    }        public Automobile(String model, int rating, boolean isTruck) {       this.model = model;       this.rating = rating;       this.isTruck = isTruck;    }                public String getModel()...

  • cant understand why my toString method wont work... public class Matrix { private int[][] matrix; /**...

    cant understand why my toString method wont work... public class Matrix { private int[][] matrix; /** * default constructor -- * Creates an matrix that has 2 rows and 2 columns */ public Matrix(int [][] m) { boolean valid = true; for(int r = 1; r < m.length && valid; r++) { if(m[r].length != m[0].length) valid = false; } if(valid) matrix = m; else matrix = null; } public String toString() { String output = "[ "; for (int i...

  • I need this program converted to c++ Please help if you can import java.util.*; // Add...

    I need this program converted to c++ Please help if you can import java.util.*; // Add two arrays of the same size (size) // Each array is a representation of a natural number // The returned array will have the size of (size + 1) elements public class Fibonacci { private static String arrToString(int[] arr) {           String s = "";           for (int i = 0; i < arr.length; i++) {               s = s + arr[i];           }...

  • import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; public class FindWordInMaze { private char grid[][]; private...

    import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; public class FindWordInMaze { private char grid[][]; private ArrayList<String> words; private HashSet<String> foundWords = new HashSet<String>(); public FindWordInMaze(char[][] grid) { this.grid = grid; this.words = new ArrayList<>();    // add dictionary words words.add("START"); words.add("NOTE"); words.add("SAND"); words.add("STONED");    Collections.sort(words); } public void findWords() { for(int i=0; i<grid.length; i++) { for(int j=0; j<grid[i].length; j++) { findWordsFromLocation(i, j); } }    for(String w: foundWords) { System.out.println(w); } } private boolean isValidIndex(int i, int j, boolean visited[][]) { return...

  • Write Junit Test for the following class below: public class Turn {    private int turnScore;    private Dice dice;    private boolean isSkunk;    private boolean isDoubleSkunk;    public Turn()    {...

    Write Junit Test for the following class below: public class Turn {    private int turnScore;    private Dice dice;    private boolean isSkunk;    private boolean isDoubleSkunk;    public Turn()    {        dice = new Dice();    }    public Turn(Dice dice) {        this.dice = dice;    }       public void turnRoll()    {        dice.roll();        if (dice.getDie1Value() == 1 || dice.getDie2Value() == 1 && dice.getLastRoll() != 2)        {            turnScore = 0;            isSkunk = true;        }        else if (dice.getLastRoll() == 2)        {            turnScore = 0;            isDoubleSkunk = true; }        else        {           ...

  • [JAVA] help —————— ListArrayMain.java ——————- public class ListArrayMain { public static void main (String[] args) {...

    [JAVA] help —————— ListArrayMain.java ——————- public class ListArrayMain { public static void main (String[] args) { ListArray L1 = new ListArray(); // constructs the list L1 L1.InsertBegin(2); L1.InsertBegin(3); L1.InsertBegin(7); L1.InsertBegin(15); L1.InsertBegin(5); L1.InsertEnd(666); L1.InsertAfter(1000,7); // Now, let's print out the array to verify the insertions worked. System.out.println("The items in the list are:"); for (int i = 0; i<= L1.lastCell; i++) { System.out.println(L1.a[i]); } System.out.println("The last cell number is: " + L1.lastCell); }// end main } ———————————- ListArray.java ———————————— public class ListArray...

  • public class File_In_36 {    private String filename;    public int[][] matrix()    {       //...

    public class File_In_36 {    private String filename;    public int[][] matrix()    {       // 1. matrix will call scan_File to determine whether or not the file       // name entered by the user actually exists       // 2. matrix will call size_matrix to determine the number of integers       // in the input file and set the instance variable matrix_size to that       // number       // 3. matrix will call check_square to determine whether or not matrix_size...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT