# Programming Assignment Solution

\$35.00 \$30.80

Category:

## Description

The following interface represents a structure that can be split into two structures of type U, one containing the rst element, and the other containing the remaining elements (see Example 1 below):

// Se pa ra bl e st ru ct ur e

p u b l i c i n t e r f a c e Separable <U >  {

• Return the length . i n t length () ;

• Return the first element . U first () ;

// Return all elements except the the first one .

U  rest () ;

// C o n c a t e n a t e the two p a r a m e t e r s .  The current object is  not changed .

U  concat ( U  s1 ,  U  s2 ) ;

}

1. Complete the class GArraybelow by implementing the methods of the interface Separable.

c l a s s GArray <T > implements Separable < GArray <T > > { p r i v a t e T [] data ;

• S u p p r e s s W a r n i n g s ( ” u nc he ck ed ” ) p u b l i c GArray ( i n t n ) {

data  =  ( T [])  new Object [ n ];

}

// Return the element at position i p u b l i c T get ( i n t i ) {

return  data [ i ];

}

// Set the element at position i p u b l i c void set ( i n t i , T e ) {

data [ i ]  =  e ;

}

• Ov er ri de

p u b l i c i n t  length ()  {

return  0;  //  Change  this

}

@ Ov er ri de

2

p u b l i c  GArray <T >  first ()  {

return  n u l l ;  //  Change  this

}

@ Ov er ri de

p u b l i c  GArray <T >  rest ()  {

return  n u l l ;  //  Change  this

}

@ Ov er ri de

p u b l i c GArray <T > concat ( GArray <T > s1 , GArray <T > s2 ) { return n u l l ; // Change this

}

}

1. Write the class Utilsthat implements the two following methods. You are free to use recursion.

 p u b l i c  c l a s s Utils { // Return the reverse  of s  without  changing  s . p u b l i c  s t a t i c >  U  reverse ( U s )  { return  n u l l ; } // Return the last  part of  s  without  changing  s. p u b l i c  s t a t i c >  U  last ( U  s ) { return  n u l l ; } }

Example 1. The class Main below shows an example of using GArray and Utils.

 p u b l i c  c l a s s Main  { p u b l i c  s t a t i c  void main ( String []  args )  { GArray < Integer >  a = new GArray < Integer >(5) ; a . set (0 , 1) ; a . set (1 , 2) ; a . set (2 , 3) ; a . set (3 , 4) ; a . set (4 , 5) ;

print ( a . first () ) ;  // prints :  1

print ( a . rest () ) ; // prints :  2  3  4  5

print ( a . concat ( a . rest () ,  a . first () ) ) ; // prints :  2  3  4  5  1

print ( Utils . reverse ( a ) ) ;  // prints :  5  4  3  2  1

print ( Utils . last ( a ) ) ;  // prints :  5

print ( a ) ;  // prints :  1  2  3  4  5

}

p u b l i c s t a t i c <T >  void print ( GArray <T >  a )  {

f o r  ( i n t  i  =  0;  i  <  a . length () ;  i ++)  {

System . out . print (a. get (i)  +  ”  “);

}

System . out . println ();

}

}

• Deliverable and rules

You must deliver:

CSC 212 PA # 0

3

1. Source code submission to Web-CAT. You have to upload the following classes in a zipped le:

GArray Utils.

Notice that you should not upload:

The interface Separable. The class Main.

1. The speci cation given in the assignment (class and interface names, and method signatures) must not be modi ed. Any change to the speci cation results in compilation errors and consequently the mark zero.

1. All data structures used in this assignment must be implemented by the student. The use of Java collections or any other data structures library is strictly forbidden.

1. This is an individual assignment. Sharing code with other students will result in harsh penalties.

1. Posting the code of the assignment or a link to it on public servers, social platforms or any communication media including but not limited to Facebook, Twitter or WhatsApp will result in disciplinary measures against any involved parties.

1. The submitted software will be evaluated automatically using Web-Cat.

1. All submitted code will be automatically checked for similarity, and if plagiarism is con-rmed penalties will apply.

1. You may be selected for discussing your code with an examiner at the discretion of the teaching team. If the examiner concludes plagiarism has taken place, penalties will apply.

CSC 212 PA # 0