Java please.
A stack machine executes programs consisting of arithmetic commands that manipulate an internal stack.
The stack is initially empty.
The commands are:
push N ; push N onto the stack (N is any Double)
pop ; removes top element from a stack
add ; replace top two elements of the stack by their sum
mul ; replace top two elements of the stack by their product
sub ; replace top two elements of the stack by their difference: [a, b, ... ] => [a-b, ... ]
div ; replace top two elements of the stack by their quotient: [a, b, ... ] => [a/b, ... ]
For example, the following program computes 23:
(push 1, push 2, mul, push 2, mul, push 2, mul)
The following types of exceptions are thrown by the stack machine:
stack empty
stack too short (not enough elements to complete the operation)
divide by 0
syntax error
Part 1
Implement a generic stack class for the stack machine:
class Stack<T> { ??? }
Notes
·You will need to implement the following methods: push, pop, top, clear, and toString.
·pop and top may throw EmptyStack exceptions
·How will you store the elements in the stack? List? Set?
Part 2
Implement your design using your solution for part 2.
Notes
·Executing arithmetic commands may throw ShortStack exceptions if there aren't at least two elements on the stack.
·div may throw a DivByZero exception. Restore the stack before throwing this exception.
·Consider making the execute method and stack in your StackMachine class static. Then, commands can make static references to the stack without needing to have an association to the stack machine.
·The execute method of StackMachine should have separate catch clauses for each type of exception. For now it just prints the exception but in the future we could potentially have different strategies for each type of exception caught.
·After the last command is execute the stack machine should print the stack.
·Before the first command is execute the stack machine should clear the stack.
Here is my code, but I am stuck here. Please help.
class
Program{
List<Command> commands;
void excute() throws StackMachineException{
for(Command c: commands)
c.excute();
}
}
class Add extends Command{
void excute() throws StackMachineException{
}
}
class Command{
}
class Stack<T>{
}
class StackMachineException{
}
public class StackMac {
public Stack<Double> stack;
void excute(Program p)
{
}
}
And here is the test code
public class StackMackTests { // f() = 5x^2 - 3 public static void test0(Double x) { // create an empty program Program p = new Program(); // add commands to p: p.add(new Push(3.0)); p.add(new Push(x)); p.add(new Push(x)); p.add(new Push(5.0)); p.add(new Mul()); p.add(new Mul()); p.add(new Sub()); // now execute StackMachine.execute(p); } // f(x) = x^3 + 3x^2 - 2x + 1 public static void test1(Double x) { // ??? } public static void main(String[] args) { test1(2.0); // [17.0] test1(5.0); // [191.0] test0(2.0); // [17.0] test0(4.0); // [77] } }
In: Computer Science
C++ Zybook Lab
15.4 Zip code and population (class templates)
Complete the TODOs in StatePair.h and main.cpp.
StatePair.h
Define a class StatePair with two template types (T1 and T2), and constructor, mutators, accessors, and PrintInfo() member functions which will work correctly with main.cpp. Note, the three vectors (shown below) have been pre-filled with StatePair data in main() and do not require modification.
main.cpp
Complete main() to:
For FULL CREDIT on this assignment, the member functions for StatePair MUST be written outside the class StatePair { ... }; context as indicated by the TODO comments.
Ex: If the input is: 21044 the output is: Maryland: 6079602
Here's the code:
//StatePair.h
#ifndef STATEPAIR
#define STATEPAIR
template<typename T1, typename T2>
class StatePair {
// TODO: Define signatures ONLY for a constructor,
mutators,
// and accessors for StatePair which work with main.cpp
// as written
// TODO: Define a signature for the PrintInfo() method
// TODO: Define data member(s) for StatePair
};
// TODO: Complete the implementations of the StatePair methods
#endif
//main.cpp
#include<iostream>
#include <fstream>
#include <vector>
#include <string>
#include "StatePair.h"
using namespace std;
int main() {
ifstream inFS; // File input stream
int zip;
int population;
string abbrev;
string state;
unsigned int i;
// ZIP code - state abbrev. pairs
vector<StatePair <int, string>>
zipCodeState;
// state abbrev. - state name pairs
vector<StatePair<string, string>>
abbrevState;
// state name - population pairs
vector<StatePair<string, int>>
statePopulation;
// Fill the three ArrayLists
// Try to open zip_code_state.txt file
inFS.open("zip_code_state.txt");
if (!inFS.is_open()) {
cout << "Could not open file
zip_code_state.txt." << endl;
return 1; // 1 indicates
error
}
while (!inFS.eof()) {
StatePair <int, string>
temp;
inFS >> zip;
if (!inFS.fail()) {
temp.SetKey(zip);
}
inFS >> abbrev;
if (!inFS.fail()) {
temp.SetValue(abbrev);
}
zipCodeState.push_back(temp);
}
inFS.close();
// Try to open abbreviation_state.txt file
inFS.open("abbreviation_state.txt");
if (!inFS.is_open()) {
cout << "Could not open file
abbreviation_state.txt." << endl;
return 1; // 1 indicates
error
}
while (!inFS.eof()) {
StatePair <string, string>
temp;
inFS >> abbrev;
if (!inFS.fail()) {
temp.SetKey(abbrev);
}
getline(inFS, state); //flushes
endline
getline(inFS, state);
state = state.substr(0,
state.size()-1);
if (!inFS.fail()) {
temp.SetValue(state);
}
abbrevState.push_back(temp);
}
inFS.close();
// Try to open state_population.txt file
inFS.open("state_population.txt");
if (!inFS.is_open()) {
cout << "Could not open file
state_population.txt." << endl;
return 1; // 1 indicates
error
}
while (!inFS.eof()) {
StatePair <string, int>
temp;
getline(inFS, state);
state = state.substr(0,
state.size()-1);
if (!inFS.fail()) {
temp.SetKey(state);
}
inFS >> population;
if (!inFS.fail()) {
temp.SetValue(population);
}
getline(inFS, state); //flushes
endline
statePopulation.push_back(temp);
}
inFS.close();
cin >> zip;
for (i = 0; i < zipCodeState.size(); ++i) {
// TODO: Using ZIP code, find state
abbreviation
if(zipCodeState.at(i).SetValue() ==
zip)
{
abbrev = zipCodeState.at(i).SetValue();
break;
}
}
for (i = 0; i < abbrevState.size(); ++i) {
// TODO: Using state abbreviation,
find state name
if(abbrevState.at(i).SetValue() ==
abbrev){
state = abbrevState.at(i).SetValue();
break;
}
}
for (i = 0; i < statePopulation.size(); ++i)
{
// TODO: Using state name, find
population. Print pair info.
if(statePopulation.at(i).SetValue()
== state){
statePopulation.at(i).PrintInfo();
break;
}
}
}
In: Computer Science
For this assignment, you will apply what you learned in analyzing Java™ code so far in this course by writing your own Java™ program. The Java™ program you write should do the following:
Complete this assignment by doing the following:
Program: PRG/420 Week 2
* Purpose: Week 2 Coding Assignment
* Programmer: Iam A. Student
* Class: PRG/420
* Creation Date: 10/18/17
*********************************************************************
*
**********************************************************************
* Program Summary: This program demonstrates these basic Java
concepts:
* - defining variables of different types
* - if-then and if-then-else logic
* - constructing a string to display onscreen
* - switch logic
*
* To complete this assignment, you will add code where indicated.
The
* behavior of your completed assignment should be to accept an
input
* value for the number of sides of a two-dimensional figure. Based
on that value,
* your code should display the type of figure that corresponds to
the number of polygon angles
* indicated (3=triangle, 4=rectangle, etc.)
*
* Here are the specific requirements:
*
* After the user types in a value from 3 to 5 inclusive (i.e., 3,
4, or 5):
*
* 1. Your code determines whether the input value is out of range
(less than 3 or more than 5)
* and, if so, displays a meaningful error message on the screen and
ends the program.
*
* 2. Because you will be comparing a single expression (the input
value) to multiple constants (3, 4, and 5),
* your code should use a switch statement to display the following
message onscreen:
*
* If user inputs 3, onscreen message should say "A triangle has 3
sides."
* If user inputs 4, onscreen message should say "A rectangle has 4
sides."
* If user inputs 5, onscreen message should see "A pentagon has 5
sides."
*
* 3. Be sure to test your program. This means running your program
multiple
* times with test values 3, 4, 5, as well as at least two values
that fall outside that range
* (one lower than the lowest and one higher than the highest) and
making sure
* that the correct message displays for each value you input. Also
be sure
* that running your program does not cause any compiler
errors.
***********************************************************************/
package week2codingassignment;
import java.util.Scanner;
public class PRG420Week2_CodingAssignment {
public static void main(String[] args) {
String userInputStringOfAngles; // Declare a variable of type
String to capture user input
int numberOfAngles; // Declare a variable of type int to hold the
converted user input
Scanner myInputScannerInstance = new Scanner(System.in); //
Recognize the keyboard
System.out.print("Please type the integer 3, 4, or 5 and then press
Enter: "); // Prompt the user
userInputStringOfAngles= myInputScannerInstance.next(); // Capture
user input as string
numberOfAngles = Integer.parseInt(userInputStringOfAngles); //
Convert the string to a number in case this will be useful
later
// LINE 1. CODE TO DETERMINE WHETHER USER INPUT IS OUT OF BOUNDS
GOES HERE
// LINE 2. SWITCH CODE TO PRINT CORRECT "SHAPE" MESSAGE BASED ON
USER INPUT GOES HERE
}
}
In: Computer Science
A network administrator has a block of IP addresses: 10.5.4.0/23. From this block of IP addresses, he created 32 subnets of the same size with a prefix of /28. He wants to determine the directed broadcast address for each subnet. He wrote down a list of possible broadcast addresses. Select the IP addresses that do not correspond to a broadcast address for any subnets he created. Choose two options.
a) 10.5.5.207
b) 10.5.5.159
c) 10.5.5.7
d) 10.5.4.78
e) 10.5.4.47
f) 10.5.4.95
In: Computer Science
a) In a subnet, a host has been assigned the following configuration parameters: 10.42.4.232/13. What is the last valid IP address on the network that this host is a part of?
b) In a subnet, a host has been assigned the following configuration parameters: 172.20.122.6/255.255.248.0. What is the broadcast address on the network that this host is a part of?
c) In a subnet, a host has been assigned the following configuration parameters: 10.158.253.161/255.192.0.0. What is the broadcast address on the network that this host is a part of?
d) In a subnet, a host has been assigned the following configuration parameters: 192.168.27.76/28. What is the first valid IP address on the network that this host is a part of?
e) In a subnet, a host has been assigned the following configuration parameters: 10.26.22.90/12. What is the first valid IP address on the network that this host is a part of?
All answers must be in dotted decimal notation.
In: Computer Science
City 1 |
1 |
1 |
City 2 |
1 |
3 |
City 3 |
4 |
1 |
City 4 |
5 |
3 |
City 5 |
3 |
5 |
in java Create 5 city objects and initialize their location (x and y) using the above table. Then put all of the five cities in a vector.
In: Computer Science
**** I need the answer with unique words .. thank you
In your own word, explain why do designers use Denormalization? What is the limitation of using Denormalization? Name and explain a better alternative approach than Denormalization.
Answer :
to increase performance. In computing, denormalization is the process of trying to improve the read performance of a database, at the expense of losing some write performance, by adding redundant copies of data or by grouping data.[1][2] It is often motivated by performance or scalability in relational database software needing to carry out very large numbers of read operations. Denormalization differs from the unnormalized form in that denormalization benefits can only be fully realized on a data model that is otherwise normalized.
Denormalization is a database optimization technique in which we add redundant data to one or more tables. This can help us avoid costly joins in a relational database. Note that denormalization does not mean not doing normalization. It is an optimization technique that is applied after doing normalization.
In a traditional normalized database, we store data in separate
logical tables and attempt to minimize redundant data. We may
strive to have only one copy of each piece of data in
database.
For example, in a normalized database, we might have a Courses
table and a Teachers table.Each entry in Courses would store the
teacherID for a Course but not the teacherName. When we need to
retrieve a list of all Courses with the Teacher name, we would do a
join between these two tables.
In some ways, this is great; if a teacher changes is or her name,
we only have to update the name in one place.
The drawback is that if tables are large, we may spend an
unnecessarily long time doing joins on tables.
I have explained each and every part of the first and third question only according to "Chegg guidelines when multiple questions are given then only first question needs to be answered" with the help of statements attached to the answer above.
In: Computer Science
In Python Create customer information system as follows:
Ask the user to enter name, phonenumber, email. Create a file by his name and save it in the hard disk. Do this repeatedly until all the users are entered. (for example, if the user enters 10 customers’s information, there should be 10 different files created.)
Now build the customer value for a retail company as follows:
Ask the customer to enter his name. If you have his information already stored, then showing him his stored number and email. If not, ask his phone and email and save on his name.
Ask him to enter what he buying (itemname, quantity, unit price.) repeatedly until he enters all the items. Calculate the total including the tax (0.0825). Append the total price to his file.
In: Computer Science
In c++, please make it short and simple
A hotel chain needs a program which will produce statistics for the hotels it owns.
Write a program which displays the occupancy rate and other statistics for any of the chain’s large hotels (all are 20 floors with 16 rooms each floor).
In main:
-declare a 20 X 16 array of “int” named hotel to represent a hotel’s 20 floors and 16 rooms per floor.
Then in main, repeatedly:
-call a function which fills the hotel array with 1s and 0s, where 1 indicates an occupied room and 0 indicates an unoccupied room. Call a second function to validate that each entry is either 1 or 0 by having the second function access the original entry using a pointer in its parameter list.
-pass the hotel array to a third function which dynamically allocates a new size 20 “int” array with each element representing a floor of the hotel and the floor’s number of occupied rooms; place the number of occupied rooms of each floor into the new array and return it to main.
-display the floor number** and number of occupied rooms on each floor by incrementing the address of the new array.
-also calculate and display the hotel’s overall occupancy rate to 1 decimal (total rooms occupied per total rooms), and the floor number** and number of occupied rooms of the floor with the highest occupancy.
-process another hotel (unknown number of hotels in chain).
**NOTE: hotels do not have a floor numbered 13 due to guests’ superstitions.
To create your screen print, temporarily set the floors and rooms to 3 and 5 respectively to reduce data entry, and enter:
Floor #1: occupied, occupied, unoccupied, occupied, unoccupied
Floor #2: occupied, unoccupied, occupied, occupied, occupied
Floor #3: occupied, unoccupied, unoccupied, unoccupied, occupied
In: Computer Science
What are the enumerators for the following enumeration data declaration?
A)enum test {RED, YELLOW, BLUE, BLACK};
B)enum test {RED, YELLOW=4, BLUE, BLACK};
C)enum test {RED=-1, YELLOW,BLUE, BLACK};
D)enum test {RED, YELLOW=6, BLUE, BLACK};
In: Computer Science
Develop a VPython program creating an animation of your group’s object (a person walking up a flight up stairs) and cause it to move so that the animation simulates the motion of the object assigned. A .py file
In: Computer Science
Q1
Create a program that asks the user to input their budget. Then, it will ask the user to input each of their purchase until their entire budget is used up.
What kind of loop do you think is the most appropriate? You don't have to create the entire program. Part of it has already been created for you (see code below).
Note: Assume that the user always uses up their budget to the last cent and they don't over spend (is that really possible? :D )
You can use the following screen output to guide your program.
Please enter your budget: $2500.25 Please enter your purchase amount: $1200 Remaining balance: $1300.25 Please enter your purchase amount: $300.25 Remaining balance: $1000.00 Please enter your purchase amount: $1000.00 Remaining balance: $0.00
Skeleton code:
#include <iostream> #include <iomanip> int main() { double budget = 0, purchase = 0; std::cout << std::fixed << std::setprecision(2); std::cout << "Please enter your budget: $"; std::cin >> budget; // place code here return 0; }
Q2
Create a program that computes the summation of a number. For example, the summation of 3 would be 1 + 2 + 3.
What kind of loop do you think is the most appropriate? You don't have to create the entire program. Part of it has already been created for you (see code below).
You can use the following screen output to guide your program.
summation(n) program Please enter n: 3 The summation(3) is 6
Skeleton code:
#include <iostream> #include <iomanip> int main() { int summation = 0, n = 0; std::cout << "summation(n) program"; std::cout << "Please enter n: "; std::cin >> n;
// provide code here
std::cout << "summation("<< n << ") is: " << summation << "\n"; return 0; }
Q3
Question text
Create a program that asks the user to add all the values that a user inputs until they input -1.
What kind of loop do you think is the most appropriate? You don't have to create the entire program. Part of it has already been created for you (see code below).
Note: Be careful not to add the extra -1.
You can use the following screen output to guide your program.
Welcome to Summer-upper! Please enter a number you want to add (type -1 to exit): 10 Please enter a number you want to add (type -1 to exit): 3 Please enter a number you want to add (type -1 to exit): 11 Please enter a number you want to add (type -1 to exit): -1 Sum is: 24
Skeleton code:
#include <iostream> int main() { int sum = 0, input = 0; std::cout << "Welcome to Summer-upper!\n"; // provide code here std::cout << "Sum is: " << sum << "\n"; return 0; }
In: Computer Science
Define the following terms.
a) Heuristics b) Beam search c) Expert systems d) Best first search e) Blind search f) Informed search
In: Computer Science
Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the computer through a user interface. The user will choose to throw Rock, Paper or Scissors and the computer will randomly select between the two. In the game, Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. The program should then reveal the computer's choice and print a statement indicating if the user won, the computer won, or if it was a tie. Allow the user to continue playing until they chooses to stop. Once they stop, print the number of user wins, losses, and ties.
In: Computer Science
Programming In C
Write a program that prints the values in an array and the addresses of the array’s elements using four different techniques, as follows:
Learning Objectives
In this assignment, you will:
Requirements
Your code must use these eight functions, using these names (in addition to main):
Requirements for the printValuesNameIndex Function
Purpose: |
This function prints the values in the array. |
Parameter: |
integer array (square bracket notation) |
Algorithm: |
Print the name of the function, then print the values of the
input array with array index notation. You must use the parameter.
Do not declare any other variables except a loop control
variable. |
Return value: |
None |
Requirements for the printValuesPointerIndex Function
Purpose: |
This function prints the values in the array. |
Parameter: |
Pointer to an int array |
Algorithm: |
Print the name of the function, then print the values of the
input array with pointer index notation. You must use the
parameter. Do not declare any other variables except a loop control
variable. |
Return value: |
None |
Requirements for the printValuesNameOffset Function
Purpose: |
This function prints the values in the array. |
Parameter: |
integer array (square bracket notation) |
Algorithm: |
Print the name of the function, then print the values of the
input array with pointer offset notation using the array name. You
must use the parameter. Do not declare any other variables except a
loop control variable. |
Return value: |
None |
Requirements for the printValuesPointerOffset Function
Purpose: |
This function prints the values in the array. |
Parameter: |
Pointer to an int array |
Algorithm: |
Print the name of the function, then print the values of the
input array with pointer offset notation using the pointer. You
must use the parameter. Do not declare any other variables except a
loop control variable. |
Return value: |
None |
Requirements for the printAddressesNameIndex Function
Purpose: |
This function prints the address of each element of the array. |
Parameter: |
integer array (square bracket notation) |
Algorithm: |
Print the name of the function, then print the address of each
element of the array with array index notation. You must use the
parameter. Do not declare any other variables except a loop control
variable. Use the appropriate format control string for printing
addresses. |
Return value: |
None |
Requirements for the printAddressesPointerIndex Function
Purpose: |
This function prints the address of each element of the array. |
Parameter: |
Pointer to an int array |
Algorithm: |
Print the name of the function, then print the address of each
element of the array with pointer index notation. You must use the
parameter. Do not declare any other variables except a loop control
variable. Use the appropriate format control string for printing
addresses. |
Return value: |
None |
Requirements for the printAddressesNameOffset Function
Purpose: |
This function prints the address of each element of the array. |
Parameter: |
integer array (square bracket notation) |
Algorithm: |
Print the name of the function, then print the address of each
element of the array with pointer offset notation using the array
name. You must use the parameter. Do not declare any other
variables except a loop control variable. Use the appropriate
format control string for printing addresses. |
Return value: |
None |
Requirements for the printAddressesPointerOffset Function
Purpose: |
This function prints the address of each element of the array. |
Parameter: |
Pointer to an int array |
Algorithm: |
Print the name of the function, then print the address of each
element of the array with pointer offset notation using the
pointer. You must use the parameter. Do not declare any other
variables except a loop control variable. Use the appropriate
format control string for printing addresses. |
Return value: |
None |
Requirements for main:
Other Requirements for this Program
No other variables should be declared in main or in any function or in any function parameter list, except those explicitly noted in the instructions.
You must a symbolic constant for the array size wherever it is needed.
All output must display exactly as it appears in the sample run. Note that your actual addresses may differ.
Sample Run
Printing array values from function printValuesNameIndex
a[0] = 10
a[1] = 20
a[2] = 30
a[3] = 40
a[4] = 50
Printing array values from function printValuesPointerIndex
a[0] = 10
a[1] = 20
a[2] = 30
a[3] = 40
a[4] = 50
Printing array values from function printValuesNameOffset
a[0] = 10
a[1] = 20
a[2] = 30
a[3] = 40
a[4] = 50
Printing array values from function printValuesPointerOffset
a[0] = 10
a[1] = 20
a[2] = 30
a[3] = 40
a[4] = 50
Printing array addresses from function printAddressesNameIndex
&a[0] = 62fe00
&a[1] = 62fe04
&a[2] = 62fe08
&a[3] = 62fe0c
&a[4] = 62fe10
Printing array addresses from function printAddressesPointerIndex
&a[0] = 62fe00
&a[1] = 62fe04
&a[2] = 62fe08
&a[3] = 62fe0c
&a[4] = 62fe10
Printing array addresses from function printAddressesNameOffset
&a[0] = 62fe00
&a[1] = 62fe04
&a[2] = 62fe08
&a[3] = 62fe0c
&a[4] = 62fe10
Printing array addresses from function printAddressesPointerOffset
&a[0] = 62fe00
&a[1] = 62fe04
&a[2] = 62fe08
&a[3] = 62fe0c
&a[4] = 62fe10
In: Computer Science