Test Memory Usage on the Text File Reversing Problem (1
mark)
Test the performance of your Stack implementation on file reversing
task. The code below is to reverse a text file using the stack. It
has three lines missing, and you need to complete it. Then you
submit it on our marking site here. You need to change the stack
capacity according to input data size. On average each line has
about 11 words. Our site prints out the total computer memory usage
by your program when it runs on 5 data sets of different
sizes.
The total memory usage from the submission site is
static String[] reverse(String filename) throws Exception{
Scanner scanner = new Scanner(new
File(filename)).useDelimiter("[^a-zA-Z]+");
Stack2540Array stack = new
Stack2540Array();
while (scanner.hasNext())
stack.push(scanner.next().toLowerCase());
String[] rev = new
String[stack.size()];
/* for (int i = 0; i < stack.size(); i++) {
rev[i] =
stack.pop();
} */
return rev;
}
The code for Stack2540Array cannot be modified:
import java.io.*;
import java.util.*;
public class Stack2540Array {
int CAPACITY = 128;
int top;
String[] stack;
public Stack2540Array() {
stack = new String[CAPACITY];
top = -1;
}
public int size() {
return top + 1; }
public boolean isEmpty() {
return (top == -1); }
public String top() {
if (top == -1)
return
null;
return stack[top];
}
public void push(String element) {
top++;
stack[top] = element;
}
public String pop() {
if (top == -1) {
return null;
}
else {
return stack[top--];
}
}
}
The lines are added as comments, but the output shows: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 128 out of bounds for length 128. Your Stack and reverse() can run, but the result is not correct. Can you please fix the error in the code?
* The complete program is not available as we are supposed to work with code fragments.
In: Computer Science
“Software Integration” is a procedure of merging two or more different software structures either mono-directional or bi-directional so that information and functionality flow among that system efficiently.
Using your own words, explain “Why there is a need for Software Integration” and list some cases where “Software Integration” can help the organizations.
In: Computer Science
Write a jQuery click event handler for all tags within
elements. In the handler, output the src attribute of the image to the console.
In: Computer Science
Show how the Shell sort algorithm sorts the array {36, 14, 27, 40, 31, 17, 5, 9, 38, 22, 11}. Trace through the algorithm. Must show the operation of the algorithm in details.
In: Computer Science
I'm in a intro to programming class using C++. In some examples in my book, they use the letter ' i ' to do certain things like a 'for loop'. I was looking around the internet and I can't seem to find what that specific letter does. In all the websites I went to about operators it wasn't listed. Do I need to define it? Or just use it like it is?
Example: for(int i = 0; i < 4; i++)
In: Computer Science
In: Computer Science
Create a 5-step modelling process that can be used to create an enterprise model. Pay specific attention the aspects of the purpose of modelling and at least 5 steps to follow to create the model [20 marks] - be guided by mark allocation
In: Computer Science
Complete the following four installations: Virtual Box: o Install Virtual Box. o Capture a screen shot . Windows OS Workstation: o Create one Windows OS workstation: You can use windows 7, 8 or 10 (If you have a DVD with OS use the DVD). o Capture a screen shot. Windows Server Image: o Create one Windows Server (2008, 2012 or 2016) images in the workstation you created. Choose any operating system you want. Packet Tracer: o Install Packet Tracer on your host machine. please post screen shots of each
In: Computer Science
Please write in Python(Python3)
Stack: The following was done already in the Lab in the Stacks Module with Doubly Linked List.(***Code Below***)
Create an application to help you stack and un-stack containers in the ship.
Create a class called container which will have the object (data), the link (next)
Create a class called Pod which is Stack. Include methods addContainer and removeContainer
Implement these classes by creating multiple containers to go inside the pod.
ADD the Following feature:(*This is what I need to ADD to the code.)
Include a class attribute in the container class called name.
In the implementation - Pod:
You should ask the user to enter the name of the container and the program should add the container to the Pod repeatedly.
Once done, You should ask the user which container to remove from the Pod. When the user gives the name of the container, the program should go through the Pod and remove that container alone from the Pod.
Clue: Remember if you want to remove one item from the Stack, you should use only the top to remove. So if you want to remove the fifth item from the top of the Stack, you should remove all the four items above it, remove the fifth item and then put back all the four items in the same order.
Write a method called removeNamedContainer in Pod program for implementing the above.
# DSTACK #
#****************************************************
# the container
class Container:
# initialise the container object
def __init__(self, data):
self.data = data
self.next = None
self.prev = None
# The pod
class Pod:
stack=[]
# push item to the stack
def addContainer(self, data):
c = Container(data)
c.next = None
self.stack.append(c)
# set the next item and prevous
length = len(self.stack)
# print(str(length))
if length>1:
self.stack[length-2].next=self.stack[length-1]
self.stack[length-1].prev=self.stack[length-2]
# pop the stack
def removeContainer(self):
self.stack.pop()
# reset the next link
length = len(self.stack)
self.stack[length-1].next=None
# to print items
def printList(self):
print("Traversal in forward direction")
temp = self.stack[0]
while (temp):
print(temp.data)
temp = temp.next
print("Traversal in reverse direction")
temp = self.stack[len(self.stack)-1]
while (temp):
print(temp.data)
temp = temp.prev
break
# Testing the stack
print("Adding containers...")
pod = Pod()
pod.addContainer(1)
pod.addContainer(2)
pod.addContainer(3)
pod.printList()
# poping the stack
print("Removing containers...")
pod.removeContainer()
pod.printList()
In: Computer Science
Develop a program that creates just 3 identical arrays, mylist_1, mylist_2, and mylist_3, of just 250 items. For this assignment, the program then will sort mylist_1 using a bubble sort, mylist_2 using a selection sort, and mylist_3 will be using an insertion sort and outputs the number of comparisons and item assignments made by each sorting algorithm.
template <class elemType>
int seqSearch(const elemType list[], int length, const
elemType& item)
{
int loc;
bool found = false;
loc = 0;
while (loc < length && !found)
if (list[loc] == item)
found = true;
else
loc++;
if (found)
return loc;
else
return -1;
} //end seqSearch
template <class elemType>
int binarySearch(const elemType list[], int length,
const elemType& item)
{
int first = 0;
int last = length - 1;
int mid;
bool found = false;
while (first <= last && !found)
{
mid = (first + last) / 2;
if (list[mid] == item)
found = true;
else if (list[mid] > item)
last = mid - 1;
else
first = mid + 1;
}
if (found)
return mid;
else
return -1;
} //end binarySearch
template <class elemType>
void bubbleSort(elemType list[], int length)
{
for (int iteration = 1; iteration < length; iteration++)
{
for (int index = 0; index < length - iteration;
index++)
{
if (list[index] > list[index + 1])
{
elemType temp = list[index];
list[index] = list[index + 1];
list[index + 1] = temp;
}
}
}
} //end bubbleSort
template <class elemType>
void selectionSort(elemType list[], int length)
{
int minIndex;
for (int loc = 0; loc < length; loc++)
{
minIndex = minLocation(list, loc, length - 1);
swap(list, loc, minIndex);
}
} //end selectionSort
template <class elemType>
void swap(elemType list[], int first, int second)
{
elemType temp;
temp = list[first];
list[first] = list[second];
list[second] = temp;
} //end swap
template <class elemType>
int minLocation(elemType list[], int first, int last)
{
int minIndex;
minIndex = first;
for (int loc = first + 1; loc <= last; loc++)
if (list[loc] < list[minIndex])
minIndex = loc;
return minIndex;
} //end minLocation
template <class elemType>
void insertionSort(elemType list[], int length)
{
for (int firstOutOfOrder = 1; firstOutOfOrder < length;
firstOutOfOrder++)
if (list[firstOutOfOrder] < list[firstOutOfOrder - 1])
{
elemType temp = list[firstOutOfOrder];
int location = firstOutOfOrder;
do
{
list[location] = list[location - 1];
location--;
}
while(location > 0 && list[location - 1] >
temp);
list[location] = temp;
}
} //end insertionSort
template <class elemType>
void quickSort(elemType list[], int length)
{
recQuickSort(list, 0, length - 1);
} //end quickSort
template <class elemType>
void recQuickSort(elemType list[], int first, int last)
{
int pivotLocation;
if (first < last)
{
pivotLocation = partition(list, first, last);
recQuickSort(list, first, pivotLocation - 1);
recQuickSort(list, pivotLocation + 1, last);
}
} //end recQuickSort
template <class elemType>
int partition(elemType list[], int first, int last)
{
elemType pivot;
int smallIndex;
swap(list, first, (first + last) / 2);
pivot = list[first];
smallIndex = first;
for (int index = first + 1; index <= last;
index++)
if (list[index] < pivot)
{
smallIndex++;
swap(list, smallIndex, index);
}
swap(list, first, smallIndex);
return smallIndex;
} //end partition
template <class elemType>
void heapSort(elemType list[], int length)
{
buildHeap(list, length);
for (int lastOutOfOrder = length - 1; lastOutOfOrder
>= 0;
lastOutOfOrder--)
{
elemType temp = list[lastOutOfOrder];
list[lastOutOfOrder] = list[0];
list[0] = temp;
heapify(list, 0, lastOutOfOrder - 1);
}//end for
}//end heapSort
template <class elemType>
void heapify(elemType list[], int low, int high)
{
int largeIndex;
elemType temp = list[low]; //copy the root node of
//the subtree
largeIndex = 2 * low + 1; //index of the left child
while (largeIndex <= high)
{
if (largeIndex < high)
if (list[largeIndex] < list[largeIndex + 1])
largeIndex = largeIndex + 1; //index of the
//largest child
if (temp > list[largeIndex]) //subtree
//is already in a heap
break;
else
{
list[low] = list[largeIndex]; //move the larger
//child to the root
low = largeIndex; //go to the subtree to
//restore the heap
largeIndex = 2 * low + 1;
}
}//end while
list[low] = temp; //insert temp into the tree,
//that is, list
}//end heapify
template <class elemType>
void buildHeap(elemType list[], int length)
{
for (int index = length / 2 - 1; index >= 0; index--)
heapify(list, index, length - 1);
}
In: Computer Science
create a C++ Program
1. Ask and get a course name
2. Create an array of students of size 10,
3. Initialize the elements of the students array of appropriate names and grades
4. Create an object of class GradeBook (provide the course name and the created student array, in 3 above, as arguments to the constructor call. The arguments are used to initialize the data members of the class GradeBook.
Desired Output:
=========================================================
Enter course name: Object Oriented Programming
=====================Entering Students' Information===============================
Enter the name and grade for 10 students
student # 1 name: John
Student # 1 grade : 100
student # 2 name: Mark
Student # 2 grade : 100
student # 3 name: Jesus
Student # 3 grade : 89
student # 4 name: Tony
Student # 4 grade : 87
student # 5 name: Leo
Student # 5 grade : 79
student # 6 name: Don
Student # 6 grade : 75
student # 7 name: Devin
Student # 7 grade : 83
student # 8 name: Xavier
Student # 8 grade : 90
student # 9 name: jerry
Student # 9 grade : 25
student # 10 name: Jones
Student # 10 grade : 46
============================================================================
Welcome to the grade book for
Object Oriented Programming!
=====================After Processing Class's Grade===============================
The grades are:
Jonh : 100
Mark : 100
Jesus: 89
Tony : 87
Leo: 79
Don : 75
Devin: 83
Xavier : 90
Jerry : 25
Jones : 46
Class average is 77.40
Lowest grade is 25
Highest grade is 100
Grade distribution:
0-9:
10-19:
20-29: *
30-39:
40-49: *
50-59:
60-69:
70-79: **
80-89: ***
90-99: *
100: **
Press any key to continue . . .
SAMPLE CODE!!!!!!!!!!!!!!!!!!!!!!!!
GradeBook.h
|
#pragma once #include<string> #include<array> class GradeBook { public: GradeBook(std::string& cName,std::array<int,10>& sGrades) : courseName{ cName }, studentGrades{ sGrades } { } std::string getCourseName() const { return courseName; } void setCourseName(const std::string& cName) { courseName = cName; } void processGrades() const { outputGrades(); std::cout << "\nClass average: " << getAverage() << std::endl; std::cout << "\nClass maximum: " << getMaximum() << std::endl; std::cout << "\nClass minimum: " << getMinimum() << std::endl; std::cout << "Bar Chart:\n"; outputBarChart(); } int getMaximum() const { int highGrade{ 0 }; //range-based for loop for (int grade : studentGrades) { if (highGrade < grade) { highGrade = grade; } } return highGrade; } int getMinimum() const { int lowGrade{ 100 }; for (int grade : studentGrades) { if (lowGrade > grade) { lowGrade = grade; } } return lowGrade; } double getAverage() const { int sum{ 0 }; for (int grade : studentGrades) { sum += grade; } return static_cast<double>(sum) / studentGrades.size(); } void outputGrades() const { std::cout << "\n The grades are: \n\n"; for (size_t i{ 0 }; i < studentGrades.size(); ++i) { std::cout <<"Student "<< i + 1 << " grade: " << studentGrades.at(i) << std::endl; } } void outputBarChart() const { std::cout << "\nGrade distribution:\n"; std::array<int, 11> frequency{}; for (int grade : studentGrades) { ++frequency[grade / 10]; } for (size_t i{ 0 }; i < frequency.size(); ++i) { if (i == 0) { std::cout << " 0-9:"; } else if (i == 10) { std::cout << " 100:"; } else { std::cout << i * 10 << "-" << (i*10) + 9 << ":"; } for (unsigned stars{ 0 }; stars < frequency[i]; ++stars) { std::cout << '*'; } std::cout << std::endl;
} } private: std::string courseName; std::array<int, 10> studentGrades; }; |
GradeBookDriver.cpp
|
#include<iostream> #include<string> #include"GradeBook.h" #include<array> using namespace std; int main() { string courseName = "COSC 1337 Object Oriented Programming"; array<int, 10> studentGrades{ 87, 68, 94, 100, 83, 78, 85, 91, 76, 87 }; GradeBook myGradeBook(courseName,studentGrades); myGradeBook.setCourseName(courseName); myGradeBook.processGrades(); } |
In: Computer Science
Minimum Recharging Stops
You have decided that your first purchase after graduation will be
a Tesla Model X. You have further decided to drive from Harrisburg
to San Francisco, as quickly (and safely) as possible. In order to
minimize your travel time, you wish to minimize the number of
recharging stops. You have recorded the distances between charging
stations on your route, and you know the maximum range that you can
travel on a single charge.
Input: An array g containing the distances of charging stations on the route from the starting point; r the range you can travel on a single charge, and d the length of the route from Harrisburg to San Francisco. You are guaranteed that the distances between the charging stations will be less than or equal to r.
Return: The minimum number of stops needed for charging in order to complete the trip.
a) Suppose that you wrote an exhaustive search algorithm. How
many possible solutions would you examine?
b) Write an efficient algorithm to solve this problem.
In: Computer Science
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JFrame;
public class GridBagLayoutDemo {
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;
public static void addComponentsToPane(Container pane) {
if (RIGHT_TO_LEFT) {
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if (shouldFill) {
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
}
button = new JButton("Button 1");
if (shouldWeightX) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Button 2");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Button 3");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 0;
pane.add(button, c);
button = new JButton("Long-Named Button 4");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40; //make this component tall
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 1;
pane.add(button, c);
button = new JButton("5");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 0; //reset to default
c.weighty = 1.0; //request any extra vertical space
c.anchor = GridBagConstraints.PAGE_END; //bottom of space
c.insets = new Insets(10,0,0,0); //top padding
c.gridx = 1; //aligned with button 2
c.gridwidth = 2; //2 columns wide
c.gridy = 2; //third row
pane.add(button, c);
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("GridBagLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
addComponentsToPane(frame.getContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
I need someone to go through this program and right more comments that explain a lot more Please. from top to bottom Thanks
In: Computer Science
i want it in C++.You will solve the Towers of Hanoi problem in
an iterative manner (using Stack) in C++(using data
structure).
Note: you have to solve for N number of disks, 3 Towers (Stacks).
Do not use recursion.
For better understanding play the game at least once.
Link:https://www.mathsisfun.com/games/towerofhanoi.html
In: Computer Science
You are now the manager of a small team of software engineers. Some of them are fresh graduates, while some of them have a few years of working experience in the field. You are tasked with producing a small experimental FinTech (Financial Technology) mobile application. You will need to publish on both Android and iPhone platforms. You are adopting an Agile methodology, with emphasis on Test-Driven Development and extensive automated tests. The concept behind the application is very innovative, so design is constantly changing.
Your team consultant, William, believes the team should spend less time on planning and more time on producing first prototypes, and then revise our design based on further feedback. Do you agree with his approach? If yes, which practice in Agile methodology is in agreement with this approach?
In: Computer Science