Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1) Write code that creates an ArrayList object that is restricted to holding onl

ID: 3535334 • Letter: 1

Question

1) Write code that creates an ArrayList object that is restricted to holding only String objects. Add the names of three cars to the ArrayList , and then display the contents of the ArrayList.


2)

Using this partial class definition,

public class Book

{

private String title;

private String author;

private String publisher;

private int copiesSold;

}

Write a constructor for this class. The constructor should accept arguments for each field. Use the member variable names as parameter names for the constructor but use the special reference variable to prevent shadowing.
Write a copy constructor for this class.
Write accessor and mutator methods for each field
Write an equals method that returns true is 2 books (objects) have sold the same amount of copies.
Write a method that allows the contents of the object to be displayed by simply placing the object's name in a display statement. E.g.
Book a = new Book("X", "Y Z", "AW", 575);

System.out.println(a);


3)


Findthe error in the following class

public class MyClass

{

private int x;

private double y;

public void MyClass(int a, double b)

{

x = a;

y = b;

}

}

Explanation / Answer

Please rate my efforts with 5 stars :) Have spent a lot of time on it :)


1.


ArrayList<String> cars = new ArrayList<String>();

singleAddress.add("BMW");

singleAddress.add("Ford");

singleAddress.add("Ferrari");

To print:

for(int i = 0; i < cars.size(); i++) //cars name of arraylist

{

System.out.println(cars.get(i));

}


2.

The constructor would look like this

Book(String title,String author, String publisher, int copies)

{

title="X";

author="Y Z";

publisher="AW"

copies=575;

}


3. The error is that the constructor does not have a return type, not even void.

Therefore the correct class would be:

public class MyClass

{

private int x;

private double y;

public MyClass(int a, double b)

{

x = a;

y = b;

}

}


Please rate my sincere efforts :)


Cheers!