Java class, array, link list , generic class

Assignment Help JAVA Programming
Reference no: EM13456

These 14 questions covers java class, Array, link list , generic class please read carfully 

 

1. Suppose we have a class A which has a constructor that takes a single integer.

(a) After the following statements have been executed, how many A objects will exist (not counting garbage objects) and which objects are they? Explain your answer and include in your explanation a picture of Java’s memory.

A a = new A(100);

A b = new A(150);

A c = b;

b = a;

a = null;

(b) After the following statements have been executed, how many A objects will exist (not counting garbage objects) and which objects are they? Explain your answer and include in your explanation a picture of Java’s memory.

A a1 = new A(200);

A a2 = new A(250);

A a3 = a2;

a2 = a1;

a1 = null;

 

2. Here is a simple Point and Circle class.

 

class Point{

private double x, y;

public Point(double x, double y){ 

this.x = x; 

this.y = y;

}

public double getX(){

 return x;

 }

public double getY(){

 return y;

 }

}

**********************************

class Circle

{ private Point c; // center

private double r; // radius

public Circle(Point c, double r){

this.r = r;

this.c = c;

}

// more stuff

}

(a) The constructor in Circle has a “privacy leak”. Explain why. Hint: Consider the following code.

Point p = new Point(1,2);

Circle c = new Circle(p, 10);

p.setX(100);

(b) Rewrite the Circle constructor to fix this problem.

 

3. Consider this code that creates some Location objects: Location a, b, c;

a = new Location(10,20);

b = new Location(10,20);

c = b;

 

After this code executes, what are the values of these boolean expressions?explain each line what value got  to reach the value that you think.

a==b

a.equals(b)

a==c

a.equals(c)

b==c

b.equals(c)

 

4. Consider this code that creates some Location objects: Location a, b, c;

a = new Location(10,20);

b = (Location)a.clone( );

c = a;

c.shift(2,0);

 

After this code executes, what are the values of these boolean expressions? explain each line what value got  to reach the value that you think?

a==b

a.equals(b)

a==c

a.equals(c)

b==c

b.equals(c)

 

5. What does the following program print out. Explain why. Try to make comment?

Class Thing

{ public int a;

public int b;

public Thing(int a, int b){this.a=a; this.b=b}

}

public class Test

{ public static void f(Thing x, int y)

{

x.a++;

y++;

}

public static void main(String[] args)

{

Thing x = new Thing(1,1);

int y = 1;

f(x, y);

System.out.println("x.a = " + x.a + " and x.b = " + x.b);

System.out.println(" y = " + y);

}

}

 

6. (Generic Class ) Suppose that we have classes A, B, C and D. Suppose that B is a subclass of A, that C is a subclass of B, and D is a subclass of A. Suppose that we make the following declarations.

A a1 = new A();

A a2 = new C();

D d1 = new D();

 

For each part below, explain what, if any, errors would be caused by the statement in

that part. Be sure to consider both compile time and run time errors. Use tree to show you answer ?

(a) A a3 = new B();

(b) B b1 = new A();

(c) B b2 = (B) a1;

(d) B b3 = (B) a2;

(e) B b4 = (B) d1;

 

7. Suppose we implement the IntArrayBag class using two partially-filled, “parallel arrays” instead of a single (partially-filled) array. The first array, data, holds the values of the items in the bag and the second array, dataCounts, holds a count of the number of times that the associated item is in the bag. In other words, data[i] is an integer in the bag,

and dataCounts[i] is the number of times that integer is in the bag. We assume that there is an instance variable manyDataItems that tells us how many of the entries from the partially-filled arrays data and dataCounts are used to hold items from the bag (so manyDataItems <= data.length). The instance variable manyItems is a count of how many items are in the bag. We assume that the part of the array data that stores the bag does not have any duplicate entries and we assume that each value in dataCounts is strictly greater than zero for all elements with index less than manyDataItems.

a)Describe what is meant by the “capacity” of a bag in this implementation.

b) Describe an advantage that this implementation of IntArrayBag has over the single array implementation ?

(c) Describe a disadvantage that this implementation of IntArrayBag has when com- pared to the single array implementation ?

(d) Write an implementation for each of the add(int element) and remove(int target) methods ?

public class IntArrayBag { // use two partially-filled "parallel arrays"

private int[ ] data; // the data items

private int[ ] dataCounts; // how many times each item is in the bag

private int manyDataItems; // number of elements in partially-filled array

private int manyItems; // total number of items in the bag

public IntArrayBag( )

{ final int INITIAL_CAPACITY = 10;

data = new int[INITIAL_CAPACITY];

dataCounts = new int[INITIAL_CAPACITY];

manyDataItems = 0;

manyItems = 0;

}

public IntArrayBag(int initialCapacity)

{ if (initialCapacity < 0) throw new IllegalArgumentException("Capacity<0.");

data = new int[initialCapacity];

dataCounts = new int[initialCapacity];

manyDataItems = 0;

manyItems = 0;

}

/** Add a new element to this bag. If the new element would take this

* bag beyond its current capacity, then the capacity is increased. **/

public void add(int element)

{

}//add()

/** Remove one copy of a specified element from this bag.

* If target was found in the bag, then one copy of target

* has been removed and the method returns true. Otherwise

* the bag remains unchanged and the method returns false. **/

public boolean remove(int target)

{

}//remove()

}//IntArrayBag

 

8. In an implementation of linked lists, what are sentinel (or “dummy”) nodes? What is

the advantage, when implementing a linked list, of using sentinel nodes?

 

9. Suppose that a linked list is formed from objects that belong to the following class.

class ListNode

{

public int item; // An item in the list.

public ListNode next; // Reference to next item in the list.

}

Write a method that will count the number of zeros that occur in a given linked list of

ints. The method should have a parameter of type ListNode and should return a value

of type int. (Use the ListNode class exactly as it is implemented above.)

 

10. an implementation of the IntNode class as you see below . Write an implementation of a static method 

public static String list2String( IntNode node )

that returns a String representation of the linked list referred to by the parameter node.

If the linked list is empty, the String representation should be "[]" (two square brackets

next to each other). If the linked list is not empty, the String representation should

look like this, "[ 3 52 0 2 -4 16 ]", with a space before each entry of the list and a

space before the closing bracket.

 

class IntNode

{

private int data;

private IntNode link;

public IntNode(int data, IntNode link)

{

this.data = data;

this.link = link;

}

public int getData( ) { return data; }

public IntNode getLink( ) { return link; }

public void setData(int data) { this.data = data; }

public void setLink(IntNode link) { this.link = link; }

}//IntNode

 

 

11. On the implementation of the IntNode class. Write implementations for the following methods.

 

class IntNode

{

private int data;

private IntNode link;

public IntNode(int data, IntNode link)

{

this.data = data;

this.link = link;

}

public int getData( ) { return data; }

public IntNode getLink( ) { return link; }

public void setData(int data) { this.data = data; }

public void setLink(IntNode link) { this.link = link; }

}//IntNode

 

(a) Write a method

public static IntNode removeFirst( IntNode head )

 

that returns a reference to the second node from the linked list referred to by the

parameter head.

 

(b) Write a method

public static IntNode addFirst( int element, IntNode head )

 

that returns a reference to the new head of a linked list with a node containing

element followed by the list referred to by the parameter head.

 

(c) Write a method

public static void set( int element, int i )

that modifies the list referred to by the parameter head so that the i’th node in the list has its data changed to element. If there is no i’th node in the list, then the list is not modified.

12. Once again using the same  IntNode class, consider the following three lines of code.

IntNode head = new IntNode(4,new IntNode(7,new IntNode(5,new IntNode(3,null))));

IntNode ptr = head.getLink().getLink();

head.getLink().setLink( new IntNode(22, null) );

(a) Draw a picture of Java’s memory after the first line above has been executed. Be

sure to include what data is in each node.

 

(b) Draw a picture of Java’s memory after the first and second lines above have been executed.

(c) Draw a picture of Java’s memory after all three lines above have been executed.

(d) What would be a String representation for the linked list referred to by head?

(e) What would be a String representation for the linked list referred to by ptr?

(f) What would be a String representation for the linked list referred to by ptr after executing the following line (which would be executed after the above three lines)?

ptr.getLink().setLink( head.getLink() );

13. (a) In the class name IntArrayBag, explain the significance of each part of the name:

int, array, and bag.

(b) In the class name IntArraySeq, explain the significance of each part of the name:

int, array, and seq.

(c) In the class name DoubleLinkedBag, explain the significance of each part of the

name: double, linked, and bag.

 

14. Suppose that

x = y;

is a “widening” assignment.

 

(a) If x and y are primitive variables, explain why the assignment can also be referred to as a “widening conversion”.

(b) If x and y are reference variables, how are the types of x and y related to each other?

(c) If x and y are reference variables, explain why the assignment should not be referred to as a “conversion”.

(d) If x and y are reference variables, you can even make a case that the assignment should be called a “narrowing” assignment. Explain why. (Hint: What can you say about the methods callable on x as compared to the methods callable on y?)

Reference no: EM13456

Questions Cloud

Complexity of cell surfaces : Lipid rafts provide another example of the complexity of cell surfaces in both their structural character and biologic functionality. Please explain the nature of these structures and their functionality.
Biologic function relationships : Please describe how one might search for chemical structure, biologic function relationships, involving small molecular weight lipophylic compounds. Provide one example.
List the biologic functions : Lipid metabolites are often seen as key elements in cellular signaling. Is this unique? Please provide several examples of the function of lipids as key elements in signal arrays and list the biologic functions these signals affect?
Write functions : These 14 questions covers java class, Array, link list , generic class.
Java class, array, link list , generic class : These 14 questions covers java class, Array, link list , generic class.
Relationship to operations practice in the organisation : Evaluate problems in operations and identify approaches to overcoming them. Critically evaluate operating plans and identify areas for improvement. Justify, implement and evaluate changes to operations in line with modern approaches.
A report on financial accounting : This report is specific for a core understanding for Financial Accounting and its relevant factors.
Criteria when making new location decisions : Identify a number of the typical criteria used when making new location decisions
Identifies the cost of computer : identifies the cost of computer components to configure a computer system (including all peripheral devices where needed) for use in one of the following four situations:

Reviews

Write a Review

JAVA Programming Questions & Answers

  Recursive factorial program

Write a class Array that encapsulates an array and provides bounds-checked access. Create a recursive factorial program that prompts the user for an integer N and writes out a series of equations representing the calculation of N!.

  Hunt the wumpus game

Reprot on Hunt the Wumpus Game has Source Code listing, screen captures and UML design here and also, may include Javadoc source here.

  Create a gui interface

Create GUI Interface in java programing with these function: Sort by last name and print all employees info, Sort by job title and print all employees info, Sort by weekly salary and print all employees info, search by job title and print that emp..

  Plot pois on a graph

Write a JAVA program that would get the locations of all the POIs from the file and plot them on a map.

  Write a university grading system in java

University grading system maintains number of tables to store, retrieve and manipulate student marks. Write a JAVA program that would simulate a number of cars.

  Wolves and sheep: design a game

This project is designed a game in java. you choose whether you'd like to write a wolf or a sheep agent. Then, you are assigned to either a "sheep" or a "wolf" team.

  Build a graphical user interface for displaying the image

Build a graphical user interface for displaying the image groups (= cluster) in JMJRST. Design and implement using a Swing interface.

  Determine the day of the week for new year''s day

This assignment contains a java project. Project evaluates the day of the week for New Year's Day.

  Write a java windowed application

Write a Java windowed application to do online quiz on general knowledge and the application also displays the quiz result.

  Input pairs of natural numbers

Java program to input pairs of natural numbers.

  Create classes implement java interface

Interface that contains a generic type. Create two classes that implement this interface.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd