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

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 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 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...
How do I convert the integers in my 2D array list to output decimals numbers? My...
How do I convert the integers in my 2D array list to output decimals numbers? My program works accepting regular integers, but crashes when decimals are entered into my 2D array list. Can someone improve it so it accepts decimal numbers? Here is my code: import javax.swing.*; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class TwoDimArray { private int numbers[][]; public TwoDimArray() { loadArray(); } public void loadArray() { /* //loadArray() method loads the users defined filename //@return returns the...
JAVA - Quick Sort .txt and Return ----------------------------------------------------------------------------- The program should input a .txt file like...
JAVA - Quick Sort .txt and Return ----------------------------------------------------------------------------- The program should input a .txt file like below and must use a Quick sort Algorithm ================ 6 10 4 19 10 12 8 6 0 1 2 3 ================ The first number "6" represents the index of the element to return after sort the second number on the top "10" represents the number of elements or size of array. The following numbers and lines are the elements that need to go...
*****IN JAVA**** Implement a theater seating chart  as a two-dimensional array of ticket prices, like this: {10,...
*****IN JAVA**** Implement a theater seating chart  as a two-dimensional array of ticket prices, like this: {10, 10, 10, 10, 10, 10, 10, 10, 10, 10} {10, 10, 10, 10, 10, 10, 10, 10, 10, 10} {10, 10, 10, 10, 10, 10, 10, 10, 10, 10} {10, 10, 20, 20, 20, 20, 20, 20, 10, 10} {10, 10, 20, 20, 20, 20, 20, 20, 10, 10} {10, 10, 20, 20, 20, 20, 20, 20, 10, 10} {20, 20, 30, 30, 40,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT