public interface ISimpleList { protected Node start; protected Node end ; public
ID: 3687294 • Letter: P
Question
public interface ISimpleList {
protected Node start;
protected Node end ;
public int size ;
int size();
/**
* Add item e to the end of the list
* @param e
*/
void add(String e);
/**
* Get the element at position index
*
* @param index
* @return
*/
String get(int index);
/**
* Add item e at position index
*
* @param index
* @param element
*/
void add(int index, String e);
/**
* Remove the item at position index
*
* @param index
*/
void remove(int index);
/**
* Remove any occurrences of element e from the list and return how many were removed
*
* @param e the element to remove
* @return how many occurrences of e were removed
*/
int remove(String e);
}
public class Project3 {
/**
* Read lines from the file and put each line into the list in reverse order
*
* @param filename the filename
* @return A list containing the file contents in reverse order
* (first line in file is last in list and last line in file is first in list)
*/
static ISimpleList getFileContentsInReverse(String filename) {
// your code here should interact with the list using only the ISimpleList methods
//TODO:
return null;
}
public static void main(String[] args) {
// you can test your list here but I will not grade this code
}
}
Explanation / Answer
public interface ISimpleList {
int size();
/**
* Add item e to the end of the list
* @param e
*/
void add(String e);
/**
* Get the element at position index
*
* @param index
* @return
*/
String get(int index);
/**
* Add item e at position index
*
* @param index
* @param element
*/
void add(int index, String e);
/**
* Remove the item at position index
*
* @param index
*/
void remove(int index);
/**
* Remove any occurrences of element e from the list and return how many were removed
*
* @param e the element to remove
* @return how many occurrences of e were removed
*/
int remove(String e);
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------
public class ArrayBasedList implements ISimpleList{
private String arr[];
private int count;
ArrayBasedList(){
count = 0;
arr = new String[4];
}
// O(1)
public int size() {
return count;
}
// O(n)
private void ensureCapacity(){
if(count == arr.length){
String newArr[] = new String[2 * count];
for(int i = 0; i < count; ++i){
newArr[i] = arr[i];
}
arr = newArr;
}
}
// O(1) amortized
public void add(String e) {
ensureCapacity();
arr[count++] = e;
}
// O(1)
public String get(int index) {
if(index < count && index >= 0){
return arr[index];
}
else{
return null;
}
}
// O(n)
public void add(int index, String e) {
if(index <= count && index >= 0){
ensureCapacity();
for(int i = count; i > index; --i){
arr[i] = arr[i - 1];
}
arr[index] = e;
++count;
}
}
// O(n)
public void remove(int index) {
if(index < count && index >= 0){
for(int i = index; i < count - 1; ++i){
arr[i] = arr[i + 1];
}
--count;
}
}
// O(n)
public int remove(String e) {
int temp = 0;
for(int i = 0; i < count; ++i){
if(arr[i].equals(e)){
remove(i);
++temp;
--i;
}
}
return temp;
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Project3 {
/**
* Read lines from the file and put each line into the list in reverse order
*
* @param filename the filename
* @return A list containing the file contents in reverse order
* (first line in file is last in list and last line in file is first in list)
*/
// O(n ^ 2)
static ISimpleList getFileContentsInReverse(String filename) {
ISimpleList list = new ArrayBasedList();
Scanner in = null;
try {
in = new Scanner(new File(filename));
while(in.hasNextLine()){
list.add(0, in.nextLine());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
finally{
if(in != null){
try{
in.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}
return list;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String fileName;
System.out.print("Enter file name: ");
fileName = in.nextLine();
ISimpleList list = getFileContentsInReverse(fileName);
System.out.println("content of " + fileName + " in reverse");
for(int i = 0; i < list.size(); ++i){
System.out.println(list.get(i));
}
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------
RandomFile.txt:
Hi
I am Ronado
From Portugal
-------------------------------------------------------------------------------------------------------------------------------------------------------------
Output;
Enter file name: randomFile.txt
content of randomFile.txt in reverse
From Portugal
I am Ronado
Hi
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.