In: Computer Science
Generic Stack due 10/16/2020 Friday 12 pm Mountain Time
Generic Stack
Make It Generic!
The first part of this lab is making your stack generic. Take the code from your working StringStack class - everything except the main method - and paste it into the GenericStack class. Change the name of the constructors to match the new name of the class, then modify the whole class so it uses generics, and can store any type that a programmer asks for.
Numbers and Readers
To take advantage of polymorphism, Java has a lot of inheritance hierarchies built-in. You may not have known that simple numeric classes, like Integer and Float, are actually part of one! Java contains a generic number class creatively called Number, and pretty much any class in Java whose purpose is to store numbers inherits from this class.
There are many types of Readers that can extract characters from different things, such as Strings, Files, and arrays.
Scrolls of Numbers
A few years back, I bought a box of random numbers from an intrepid salesman who insisted they were special. I'd like to find out if that's really the the case. Unfortunately, I've stored the numbers in a bunch of different places - some of them are in Strings, others are in files, it's a bit of a mess.
So I created a generic calculator that, using Readers, can read in a list of numbers from any source, put them into a stack, and then add them all up. Since I'm a good programmer, I put each of these steps in their own functions... but it looks like I accidentally erased some of them. Your next task in the lab is to repair my generic stack calculator by fixing the functions.
Wrapping up
you should get output that looks somewhat like this (exact numbers may vary due to floating-point rounding):
76.4 76.39999961853027 4584425.0 15.324
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.*;
public class GenericStack {
/* YOUR CODE HERE
* Just like in the ArrayList lab, copy your StringStack code,
excluding the
* main method, here.
* Then, modify it so it's generic!
*/
public static void main(String[] args) {
// If any of these lines cause a compilation error, your stack
hasn't
// been properly made generic.
GenericStack intStack = new GenericStack<>();
GenericStack stringStack = new GenericStack<>();
GenericStack> listStack = new GenericStack<>();
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.*;
import java.io.*;
import java.math.*;
public class Calculator {
public static void main(String[] args) throws FileNotFoundException, IOException {
String numbers = "56 3 7 2.0 8.4";
char[] moreNumbers = "1.0 2 3 4 5 0.324".toCharArray();
GenericStack stack1 = makeStack(new
StringReader(numbers));
System.out.println(evaluate(stack1));
GenericStack stack2 = new GenericStack<>();
for (String token : numbers.split(" ")) {
stack2.push(Float.parseFloat(token));
}
System.out.println(evaluate(stack2));
GenericStack stack3 = makeStack(new
FileReader("numbers.txt"));
System.out.println(evaluate(stack3));
GenericStack stack4 = makeStack(new
CharArrayReader(moreNumbers));
System.out.println(evaluate(stack4));
}
/* This function is meant to take in a Reader called "reader" and
return a
* stack of Numbers.
*
* Remember: don't change anything that's already there. Just add
your new
* code where the comment is to fix the function's signature.
*/
public static /* ??? */ throws IOException {
GenericStack stack = new GenericStack<>(64);
char[] data = new char[64];
reader.read(data);
String tokens = new String(data);
for (String token : tokens.split(" ")) {
stack.push(parse(token));
}
return stack;
}
/* This function is meant to take in a stack of ANY KIND of
Number
* called "stack", and return the double you get if you add all of
the
* numbers together.
* The function must be able to accept a stack of ANY KIND of
Number!
*
* Hint: use wildcard generics!
*/
public static /* ??? */ {
/* implement me! */
return 0.0;
}
/* This function is missing a return type.
* Examine it, and see if you can tell what type it should
return.
* (Spoiler: it's probably what you think it is.)
*/
public static /* ??? */ parse(String s) {
try {
return Integer.parseInt(s);
} catch (NumberFormatException e) { }
try {
return Double.parseDouble(s);
} catch (NumberFormatException e) { }
return new BigDecimal(s);
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.*;
public class StringStack {
private String [] stack;
public int size, top;
public StringStack(int size) {
this.top=-1;
this.size=size;
stack= new String[size];
}
public StringStack() {
this.size=10;
this.top=-1;
stack= new String[this.size];
}
public boolean empty() {
return this.top==-1;
}
public String peek() {
if (this.top==-1) {
throw new
NoSuchElementException("Empty stack!");
}
else {
return this.stack[this.top];
}
}
public String pop() {
if (this.top==-1) {
throw new NoSuchElementException("Empty
stack!");
}
else {
return this.stack[this.top--];
}
}
public String push(String s) {
if (this.top>=this.size-1) {
throw new IllegalStateException("Stack is
full!");
}
this.stack[++this.top]= s;
return this.stack.toString();
}
public int search(String s) {
int dist = 0;
int top = this.top;
while(top>=0) {
if(this.stack[top]==s) {
return
dist;
}
dist++;
top--;
}
return -1;
} /* I removed the public static void main method from StringStack
Class because I was getting a prompt saying the question is too
long on Chegg there should not be an issue we just have to convert
the methods above to generic as mentioned by copy and pasting it in
GenericStack class and then modifying it. This all the
formation*/
In: Computer Science
AVL Group assignment
POST ANSWER IN JAVA PLS
Populate a tree via a text file (input.txt) Make sure that after every insert, the tree is balanced. At the end, display the tree in level format. Make sure to include the height and the balance factor of every node in your output. Redirect the display to an output file (output.txt)
Hint:
//I will not accept any other algorithm
//This is not a recursive algorithm
node * rebalance(node *node){
node->height = max(height(node->left), height(node->right)) + 1;
int balance = getBalance(node); //node->left - node->right
/*do rotations as necessary
If Left heavy outside : return rightRotate(node);
If right heavy outside: return leftRotate(node);
If left heavy inside: left rotation first, right rotation 2nd, return top node
node->left = leftRotate(node->left);
return rightRotate(node);
if right heavy inside: right rotation first, left rotation 2nd, return top node
node->right = rightRotate(node->right);
return leftRotate(node);
if no rotation, return node
*/
}
//non-tail recursive algorithm because of rebalance
node* insert(node* node, int key)
{
//recursive Code for inserting a node
//When insert happens set height to 0 for the node
if (node == NULL)
return(newNode(key));
if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);
node=rebalance(node); //update heights and rebalance
return node;
}
node *leftRotate(node *x){
struct node *y=x->right;
//add more code to rotate to the left, update heights for x and y
//return y
}
node *rightRotate(node *x){
struct node *y=x->left;
//add more code to rotate to the right, update heights for x and y
//return y
}
POST ANSWER IN JAVA PLS
In: Computer Science
discuss the main characteristics of the database approach and how it differs from traditional file systems.
In: Computer Science
what is the extended definition of MongoDB, Cassandra, influxdb, and neo4j?
In: Computer Science
In: Computer Science
Class as a ChapteredVideo. The constructor should take the following parameters:
ID, length, num_chapters, chap_lengths, location, [extension]
The new parameter should be stored as their own instance variables. The ChapteredVideo object should behave as the Video. However, the method get_ID should now return a list which contains whatever the Video previously would return as its ID as the first element, followed by a list of chapter names. Each chapter name will be formed by the ID of the main video followed by ‘#’ and then the chapter number (starting at 1). For instance, a valid list could be ["123", "123#1", "123#2", "123#3"]. The ChapteredVideo class should also have a new accessor method, get_num_chapters.
Implement ChapteredVideo class as specified above.
In: Computer Science
Topic: Database testing techniques
Write a research paper on this topic but task is that i write just
scope of this paper
Brief Idea (not more than 200 words): Define scope of your
project
List of papers which u take (Minimum of 8 paper at least 4 papers
after 2016)
Kindly provide some research paper which are related with my
topic
In: Computer Science
PLEASE READ CAREFULLY!!!!
write a client.py and server.py file for tic-tac-toe IN PYTHON with the following restrictions (SO WRITE TWO FILES THAT PLAY PYTHON THROUGH A SOCKET)
Use a 5 x 5 grid (dimensions are subject to change, so use
constants for NUM_ROWS and NUM_COLS)
Use 'X' for player 1 and 'O' for player 2 (symbols and the number
of players is subject to change, so use constants)
Each player can make 1 move per turn before having to wait for the
next player to move. If an illegal move is made, an
error message is sent to the player by the server and the player
loses the current turn
SO AGAIN TO RECAP A TIC TAC TOE PROGRAM IN PYTHON THAT ALLOWS TWO PLAYERS TO PLAY TOGETHER, a client and a server file
In: Computer Science
2) Complete the parameterized constructor that takes arguments that are used to initialize ALL class’s member variables. [2 points] 3) Complete the code for getTitle() member function. The function returns the title of the Song. [1 point] 4) Complete the code for getDurationSec() member function, which returns the duration in seconds instead of minutes. [2 points] 5) Write member function definitions for three member functions below: [3 points] void setTitle(string t); //set title to t void setAuthor(Author a); //set author to a void setDurationMin(double d); //set durationMin to d 6) Demonstrate how you can use the Song class in the main() function. Prompt users to enter the details of songs, store each song in a list (vector) then print out the Song objects from the list. The grading rubric for this question is shown below
In: Computer Science
a. Write a function sumDigits that takes a positive
integer value and returns the
total sum of the digits in the integers from 1 to that number
inclusive.
b. Write a program to input an integer n and call the above
function in part a if n
is positive, else give ‘Value must be Positive’ message.
Sample Runs:
Enter a positive integer: 1000000
The sum of the digits in the number from 1 to 1000000 is
27000001
Enter a positive integer: -642
Value must be Positive
and write e a docstring comment for this
In: Computer Science
Java or python. The clients need to be able to communicate with each other using the command line. The "local chat" program should be implemented using a client/Server model based on UDP protocol.
Requirements:
1. The chat is performed between 2 clients and not the server.
2. The server will first start up and choose a port number. Then the server prints out its IP address and port number.
3. The client then will start up and create a socket based on the information provided by the server.
4. Now the client and the server can chat with each other by sending messages over the network.
5. The client/server MUST be able to communicate with each other in a continuous manner. (e.g. One side can send multiple messages without any replies from the other side)
6. There is no requirement on what language you use to implement this project, but your program should call upon the socket API for UDP to realize the functionality.
7. You should demonstrate your project using two machines (one for the client and one for the server. Pick your virtual machine as the client to communicate with your actual machine, which is server in our project).
8. You need to open at least 2 cmd window in your virtual machine to demonstrate multiple machine communicate with your server machine (actual machine)
In: Computer Science
In this module you learned how to implement recursive functions in your C++ programs.
For this assignment, you will create a program that tests a string to see if it is a palindrome. A palindrome is a string such as “madam”, “radar”, “dad”, and “I”, that reads the same forwards and backwards. The empty string is regarded as a palindrome. Write a recursive function:
bool isPalindrome(string str, int lower, int upper)
that returns true if and only if the part of the string str in positions lower through upper (inclusive at both ends) is a palindrome. Test your function by writing a main function that repeatedly asks the user to enter strings terminated by the ENTER key. These strings are then tested for palindromicity. The program terminates when the user presses the ENTER key without typing any characters before it.
Be sure to include comments throughout your code where appropriate.
In: Computer Science
In: Computer Science
Using java.util.Stack and java.util.StringTokenizer to write a program that converts an infix expression to a postfix expression using data from infix.dat. Make sure your program can handle negative number. If the input expression can’t be converted, display error message.(1.5 points)
(Should remove parentheses)
infix.dat
5 * 6 + -10
3 - 2 + 4 7
( 3 * 4 - (2 + 5)) * 4 / 2
10 + 6 * 11 -(3 * 2 + 14) / 2
2 * (12 + (3 + 5 ) * 2
In: Computer Science