Question

In: Computer Science

Convert arrays.py to Java. ‘’’’’’ File: arrays.py An Array is like a list, but the client...

Convert arrays.py to Java.

‘’’’’’

File: arrays.py

An Array is like a list, but the client can use

Only [], len, iter, and str.

To instantiate, use

<variable> = Array(<capacity>, <optional fill value>)

The fill value is None by default.

‘’’’’’

class Array(object):

‘’’’’’Represents an array.’’’’’’

def__init__(self, capacity, fillValue = None):

               ‘’’’’’Capacity is the static size of the array.

               fillValue is place at each position.’’’’’’

               self.items = list()

               for count in range(capacity):

               self.items.append(fillValue)

def__len__(self):

               ‘’’’’’->The capacity of the array.’’’’’’

               return len(self.items)

def__str__(self):

               ‘’’’’’->The string representation of the array.’’’’’’

               return str(self.items)

def__iter__(self):

               ‘’’’’’Supports traversal with a for loop.’’’’’’

               return iter(self.items)

def__getitem__(self, index):

               ‘’’’’’Subscript operator for access at index.’’’’’’

               return self.items[index]

def__setitem__(self, index, newItem):

               ‘’’’’’Subscript operator for replacement at index.’’’’’’

               self.items[index] = newItem

Solutions

Expert Solution

Program in java is given below:

import java.util.*;
import java.util.Arrays;
class Array {
String fillval="None";
String items[];
  
public Array(int capacity,String fillval) {
int x=capacity;
items=new String[capacity];
for(int count=0;count<capacity;count++)
{
items[count]=fillval;
  
}
}
public Array(int capacity) {
int x=capacity;
items=new String[capacity];
for(int count=0;count<capacity;count++)
{
items[count]=fillval;
  
}
  
}
public int len(){
return items.length;
}
public String str()
{
return Arrays.toString(items);
}
public String iter(int index)
{
return items[index];
}
public String getitem(int index)
{
return items[index];
}
void setitem(int index,String newi)
{
  
items[index]=newi;
}
}
public class example{
public static void main(String[] args) {
Array a = new Array(5);
System.out.println(a.len());
a.setitem(1,"2");
System.out.println(a.str());
System.out.println(a.iter(2));
System.out.println(a.getitem(0));
}
}

Output:

 5 [None, 2, None, None, None] None None

Related Solutions

Hello, I need to convert this java array into an array list as I am having...
Hello, I need to convert this java array into an array list as I am having trouble please. import java.util.Random; import java.util.Scanner; public class TestCode { public static void main(String[] args) { String choice = "Yes"; Random random = new Random(); Scanner scanner = new Scanner(System.in); int[] data = new int[1000]; int count = 0; while (!choice.equals("No")) { int randomInt = 2 * (random.nextInt(5) + 1); System.out.println(randomInt); data[count++] = randomInt; System.out.print("Want another random number (Yes / No)? "); choice =...
JAVA JAVA JAVA . I need to convert a string input to int array, for example...
JAVA JAVA JAVA . I need to convert a string input to int array, for example if user enters 12 / 27 / 2020 , I want to store each value in a separate array and add them afterwards.
JAVA: Provide two different implementations, an array and a linked list, to maintain a list of...
JAVA: Provide two different implementations, an array and a linked list, to maintain a list of names (two separate programs).The following operations are available: insert rear, insert front, remove a particular element, and print the whole list. Do not implement an ADT(Do not use a class with data and operations) Just set up a fixed size array or a linked list of nodes in main and provide code in main or functions/static methods to perform insert, remove, and print. You...
Java the goal is to create a list class that uses an array to implement the...
Java the goal is to create a list class that uses an array to implement the interface below. I'm having trouble figuring out the remove(T element) and set(int index, T element). I haven't added any custom methods other than a simple expand method that doubles the size by 2. I would prefer it if you did not use any other custom methods. Please use Java Generics, Thank you. import java.util.*; /** * Interface for an Iterable, Indexed, Unsorted List ADT....
Using the implementation of the array based list given, write a CLIENT method (that is NOT...
Using the implementation of the array based list given, write a CLIENT method (that is NOT part of the class) called replace, that given a value and a position replaces the value of the element at that position. REMEMBER error checking public static void replace( List aList, int newValue, int position) public class ArraybasedList implements MyListInterface{ private static final int DEFAULTSIZE = 25; // Data members: private int currentSize; private int maxSize; private S[] elements; //default constructor has NO parameters...
IN JAVA Searching and Sorting In An Integer List File IntegerList contains a Java class representing...
IN JAVA Searching and Sorting In An Integer List File IntegerList contains a Java class representing a list of integers. The following public methods are provided: ? IntegerList(int size)—creates a new list of size elements. Elements are initialized to 0. ? void randomize()—fills the list with random integers between 1 and 100, inclusive. ? void print()—prints the array elements and indices ? int search(int target)—looks for value target in the list using a linear (also called sequential) search algorithm. Returns...
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
Java Generic 2D Linked List Problem How to convert a 1D linked List into multiple linked...
Java Generic 2D Linked List Problem How to convert a 1D linked List into multiple linked lists with sequential values together? //Example 1: [1,1,2,3,3] becomes [[1,1],[2],[3,3]] //Example 1: [1,1,2,1,1,2,2,2,2] becomes [[1,1],[2],[1,1],[2,2,2,2]] //Example 3: [1,2,3,4,5] becomes [[1],[2],[3],[4],[5]] public <T> List<List<T>> convert2D(List<T> list) { // Given a 1D, need to combine sequential values together. }
JAVA PROJECT CREATING GUI WITH ARRAY Read from a file that contains a paragraph of words....
JAVA PROJECT CREATING GUI WITH ARRAY Read from a file that contains a paragraph of words. Put all the words in an array, put the valid words (words that have only letters) in a second array, and put the invalid words in a third array. Sort the array of valid words using Selection Sort. Create a GUI to display the arrays using a GridLayout with one row and three columns. The input file Each line of the input file will...
How to read a text file and store the elements into a linked list in java?...
How to read a text file and store the elements into a linked list in java? Example of a text file: CS100, Intro to CS, John Smith, 37, 100.00 CS200, Java Programming, Susan Smith, 35, 200.00 CS300, Data Structures, Ahmed Suad, 41, 150.50 CS400, Analysis of Algorithms, Yapsiong Chen, 70, 220.50 and print them out in this format: Course: CS100 Title: Intro to CS Author: Name = John Smith, Age = 37 Price: 100.0. And also to print out the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT