Question

\\ 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 each) (i, j 5 pts each)

  1. Create the following private member variables

Data Type

String

int

boolean

int

String [ ]

Variable Name

movieName

numMinutes

isKidFriendly

numCastMembers

castMembers

  1. Create the default constructor so it will initialize the variables to the following default values.

Attribute

movieName

numMinutes

isKidFriendly

numCastMembers

castMembers

Default Value

“Flick”

0

false

0

length of 10

  1. Create the fully overloaded constructor that accepts all variables as input parameters, except numCastMembers which is calculated based on the number of cast members in the castMembers array. Movie( String movieName, int numMinutes, boolean isKidFriendly, String[] castMembers )

  1. Create setter methods for the numMinutes, movieName, and isKidFriendly variables.

  1. Create getter methods for all the variables. Make sure that the getter for castMembers returns a copy of the array.

  1. Create a method named replaceCastMember that allows the name of a castMember at an index in the castMembers array to be changed. The method should take in 2 arguments, the int index and the String castMemberName. The method should return a true if the replacement was successful, false otherwise. (Note: Only update the castMember in the array if the index is valid)

public boolean replaceCastMember(int index, String castMemberName)

  1. Create a public 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. Also, if only one of the provided arrays is null, the method returns false, if both provided arrays are null, the method returns true. (Note: this method will be used by the equals method in part h)

Hint: Both arrays should have the same number of items, and the items at each index must match, ignoring case.

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

  1. Create a method named getCastMemberNamesAsString that gets the contents of the array of castMembers as a comma separated String. Hint: no trailing comma allowed

Note: If there are no cast members in the array return “none”

  1. Create the toString() method for the Movie object such that it returns a well structured sentence containing the information for all the object’s variables.

(Note: Use the helper method getCastMemberNamesAsString created previously)

Example of the formatted String returned:

“Movie: [ Minutes 142 | Movie Name: The Shawshank Redemption | not kid friendly | Number of Cast Members: 3 | Cast Members: Tim Robbins, Morgan Freeman, Bob Gunton ]”

“Movie: [ Minutes 090 | Movie Name: Aladdin | is kid friendly | Number of Cast Members: 4 | Cast Members: Scott Weigner, Robin Williams, Linda Larkin, Jonathan Freeman ]”

  1. Create the equals(Object o) method for the Movie object, such that it returns true if the values of all the members of both the calling object and the passed in object match. Return false if any values do not match. (Note: Use the helper method named doArraysMatch created previously)

import java.util.*;
import java.util.Arrays;
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;
this.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 this.movieName;

}

// return the number of minutes

public int getNumMinutes() {

return this.numMinutes;

}

// return true if movie is kid friendly else false

public boolean isKidFriendly() {

return this.isKidFriendly;

}

// return the array of cast members

public String[] getCastMembers() {

// create a deep copy of the array

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

// copy the strings from the array to the copy

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

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

}

return copyCastMembers;

}

// return the number of cast members

public int getNumCastMembers() {

return this.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) {
if (index < 0 || index >= numCastMembers)
return false;
castMembers[index] = castMemberName;
return true;
}

// 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 " + numMinutes + " | 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,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

}

}3: Test toString() ^ 0/5 Your output Error Movie.toString() returns: Movie: [ Minutes 142 | Movie Name: The Shawshank Redempt7: Test Overloaded Constructor ^ 0/4 Your output OK overloaded constructor exists OK movieName set correctly to Flick OK numM

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

Here is the solution to your problem. If the answer is satisfying, please do an upvote. Also if you have any doubts regarding the submitted solution, please do ask in the comments. In the comments, you can also give me feedback. I will respect your suggestion and try to improve my answers in the future.

1. 2. import java.util.; import java.util.Arrays; public class Movie 3. 4. { 5. 6. 7. 8. private String movieName; private i

import java.util.*;

import java.util.Arrays;

public class Movie

{

private String movieName;

private int numMinutes;

private boolean isKidFriendly;

private int numCastMembers;

private String[] castMembers;

// default constructor

public Movie()

{

this.movieName = "Flick";

this.numMinutes = 0;

this.isKidFriendly = false;

this.numCastMembers = 0;

this.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;

this.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 this.movieName;

}

// return the number of minutes

public int getNumMinutes()

{

return this.numMinutes;

}

// return true if movie is kid friendly else false

public boolean isKidFriendly()

{

return this.isKidFriendly;

}

// return the array of cast members

public String[] getCastMembers()

{

// create a deep copy of the array

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

// copy the strings from the array to the copy

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

{

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

}

return copyCastMembers;

}

// return the number of cast members

public int getNumCastMembers()

{

return this.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)

{

if (index < 0 || index >= numCastMembers)

return false;

castMembers[index] = castMemberName;

return true;

}

// 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 " + numMinutes + " | Movie Name: %15s | ";

if (isKidFriendly)

movie += "is kid friendly | ";

else

movie += "not kid friendly | ";

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

+

" ]"; // extra space after ] removed

return String.format(movie, 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

}

}

3: Test toString() ^ 0/5 Your output Error Movie.toString() returns: Movie: [ Minutes 142 | Movie Name: The Shawshank Redempt
To remove this error, please remove extra space after ']' at line 223,
7: Test Overloaded Constructor ^ 0/4 Your output OK overloaded constructor exists OK movieName set correctly to Flick OK numM
I am not sure why you getting this one, but I have a suggestion, whenever you are accessing member variables use "this.variablename"
Add a comment
Know the answer?
Add Answer to:
\\ this is the code i need help to get this part working the rest works...
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
  • import java.util.*; public class Movie {    private String movieName; private int numMinutes; private boolean isKidFriendly;...

    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++)...

  • I need to make this code access the main method I suppose, but I don't know...

    I need to make this code access the main method I suppose, but I don't know how to make that happen... Help please! QUESTION: Write a method called switchEmUp that accepts two integer arrays as parameters and switches the contents of the arrays. Make sure that you have code which considers if the two arrays are different sizes. Instructor comment: This will work since you never go back to the main method, but if you did, it wouldn't print out...

  • I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][]...

    I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][] t) A method that returns true if from left to right in any row, the integers are increasing, otherwise false. - columnValuesIncrease(int[][] t) A method that returns true if from top to bottom in any column, the integers are increasing, otherwise false. - isSetOf1toN(int[][] t) A method that returns true if the set of integers used is {1, 2, . . . , n}...

  • The following code skeleton contains a number of uncompleted methods. With a partner, work to complete...

    The following code skeleton contains a number of uncompleted methods. With a partner, work to complete the method implementations so that the main method runs correctly: /** * DESCRIPTION OF PROGRAM HERE * @author YOUR NAME HERE * @author PARTNER NAME HERE * @version DATE HERE * */ import java.util.Arrays; import java.util.Random; import java.util.Scanner; public class ArrayExercises { /** * Given a random number generator and a length, create a new array of that * length and fill it from...

  • Hi, I am writing Java code and I am having a trouble working it out. The...

    Hi, I am writing Java code and I am having a trouble working it out. The instructions can be found below, and the code. Thanks. Instructions Here are the methods needed for CIS425_Student: Constructor: public CIS425_Student( String id, String name, int num_exams ) Create an int array exams[num_exams] which will hold all exam grades for a student Save num_exams for later error checking public boolean addGrade( int exam, int grade ) Save a grade in the exams[ ] array at...

  • 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];           }...

  • I need to program 3 and add to program 2 bellows: Add the merge sort and...

    I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array. ____________________>>>>>>>>>>>>>>>>>>>>___________________________ (This is program 2 code that I did : ) ---->>>>>> code bellow from program 2 java program - Algorithms Write a program that randomly generates 100,000 integers into an array....

  • I need help with this one method in java. Here are the guidelines. Only public Employee[]...

    I need help with this one method in java. Here are the guidelines. Only public Employee[] findAllBySubstring(String find). EmployeeManager EmployeeManager - employees : Employee[] - employeeMax : final int = 10 -currentEmployees : int <<constructor>> EmployeeManager + addEmployee( type : int, fn : String, ln : String, m : char, g : char, en : int, ft : boolean, amount : double) + removeEmployee( index : int) + listAll() + listHourly() + listSalary() + listCommision() + resetWeek() + calculatePayout() :...

  • Need help with these two questions String outputBreadthFirstSearch(): returns a string represenng a breadth first traversal....

    Need help with these two questions String outputBreadthFirstSearch(): returns a string represenng a breadth first traversal. So, for example, for the tree shown in Figure 1, the method should output the string "bcaahttaetersse" 4. String outputDepthFirstSearch(): returns a string represenng a pre order depth first traversal. So, for example, for the tree shown in Figure 1, the method should output the string "batcathateersse This is my code so far public class Trie { final TrieNode root; public Trie() { this.root...

  • I need help with this. I need to create the hangman game using char arrays. I've...

    I need help with this. I need to create the hangman game using char arrays. I've been provided with the three following classes to complete it. I have no idea where to start. HELP!! 1. /** * This class contains all of the logic of the hangman game. * Carefully review the comments to see where you must insert * code. * */ public class HangmanGame { private final Integer MAX_GUESSES = 8; private static HangmanLexicon lexicon = new HangmanLexicon();...

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
ADVERTISEMENT