C++ Zybook Lab 16.5 LAB: Exception handling to detect input string vs. int
The given program reads a list of single-word first names and ages (ending with -1), and outputs that list with the age incremented. The program fails and throws an exception if the second input on a line is a string rather than an int. At FIXME in the code, add a try/catch statement to catch ios_base::failure, and output 0 for the age.
Ex: If the input is:
Lee 18 Lua 21 Mary Beth 19 Stu 33 -1
then the output is:
Lee 19 Lua 22 Mary 0 Stu 34
Here's the code:
#include <string>
#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
string inputName;
int age;
// Set exception mask for cin stream
cin.exceptions(ios::failbit);
cin >> inputName;
while(inputName != "-1") {
// FIXME: The following line will throw an ios_base::failure.
// Insert a try/catch statement to catch the exception.
// Clear cin's failbit to put cin in a useable state.
try{
//Reading age
cin >> age;
//Printing Name and age incremented by 1 if correct values are
entered
cout << inputName << " " << (age + 1) <<
endl;
}
catch(ios_base::failure){
//Clear cin
cin.clear();
}
cin >> age;
cout << inputName << " " << (age + 1) <<
endl;
cin >> inputName;
}
return 0;
}
In: Computer Science
There are at least five (probably more) places in the attached C# program code that could be optimized. Find at least five things in the Program.cs file that can be changed to make the program as efficient as possible.
using System;
namespace COMS_340_L4A
{
class Program
{
static void Main(string[] args)
{
Console.Write("How many doughnuts do you need for the first
convention? ");
int.TryParse(Console.ReadLine(), out int total1);
Console.Write("How many doughnuts do you need for the second
convention? ");
int.TryParse(Console.ReadLine(), out int total2);
int totalDonuts = Calculator.CalculateSum(new int[] { total1, total2 });
Console.WriteLine($"{ totalDonuts } doughnuts is {
Calculator.ConvertToDozen(totalDonuts)} dozen.");
Console.WriteLine("Press Enter to quit...");
Console.ReadLine();
}
}
static class Calculator
{
static int DOZEN = 12;
public static int CalculateSum(int[] args)
{
int return_value = 0;
foreach (int i in args)
{
return_value += i;
}
return return_value;
}
public static int ConvertToDozen(int total)
{
int dozen = DOZEN;
return total / dozen;
}
}
}
In: Computer Science
Web Server Infrastructure
Web application infrastructure includes sub-components and external applications that provide efficiency, scalability, reliability, robustness, and most critically, security.
Use the graphic below to answer the following questions:
|
Stage 1 |
Stage 2 |
Stage 3 |
Stage 4 |
Stage 5 |
|
Client |
Firewall |
Web Server |
Web Application |
Database |
In: Computer Science
1. Acting Secretary Modly violated one of the accepted principles of good leadership, “praise in public and criticize in private” (ideally one-on-one). What are a few likely reasons that some leaders succumb to the impulse to be harsh and criticize subordinates in public?
2. Before his apology, Acting Secretary Modly issued a statement earlier in the day about his remarks aboard the ship, saying he spoke from the heart and that his words were meant for the crew. “I stand by every word I said, even, regrettably any profanity that may have been used for emphasis,” he said. “Anyone who has served on a Navy ship would understand.” In other words, it’s OK to use profanity in the Navy Why do leaders often use the “acted-of-normalcy” defense (i.e., this is how it’s done around here) to justify their bad actions?
3. Acting Secretary Modly told the crew of the USS Roosevelt not to speak to the media, which he said holds a political agenda. Mr. Modly himself has conducted interviews with Reuters and the Washington Post and published the letter to the editor in the New York Times. Why do some leaders feel that they can operate under different rules than those whom they lead? What steps can a leader take to protect him/herself from these types of tendencies?
In: Operations Management
IN JAVA
12.11 PRACTICE: Branches*: Listing names
A university has a web page that displays the instructors for a course, using the following algorithm: If only one instructor exists, the instructor's first initial and last name are listed. If two instructors exist, only their last names are listed, separated by /. If three exist, only the first two are listed, with "/ …" following the second. If none exist, print "TBD". Given six words representing three first and last names (each name a single word; latter names may be "none none"), output one line of text listing the instructors' names using the algorithm. If the input is "Ann Jones none none none none", the output is "A. Jones". If the input is "Ann Jones Mike Smith Lee Nguyen" then the output is "Jones / Smith / …".
Hints:
Use an if-else statement with four branches. The first detects the situation of no instructors. The second one instructor. Etc.
Detect whether an instructor exists by checking if the first name is "none".
CODE GIVEN:
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String firstName1, lastName1;
String firstName2, lastName2;
String firstName3, lastName3;
firstName1 = scnr.next();
lastName1 = scnr.next();
firstName2= scnr.next();
lastName2= scnr.next();
firstName3= scnr.next();
lastName3= scnr.next();
/* Type your code here. */
}
}
In: Computer Science
Please select an organization of your choice from the one of the following industries:
A. Automotive B. High Tech C. Hospitality and Tourism
to develop a Benchmarking report where you need to identify:
1. The rationale for and the objectives of the benchmarking. Why is this benchmarking exercise important for the business? What type of benchmarking is appropriate (competitive, internal, functional, generic). Please remember:
2. The elements that you want to compare (for example: business units, departments, activties, employee performance etc.). You can benchmark product performance (sales, revenues, customer satisfaction, manufacturing cyscle time) , employee perfomance ( Productivity levels, untis produced per week, internal promotions rate, retention level, absenteeism level), customer service (digital customer service, customer satisfaction score, customer problem solving score).
3. The performance indicators (metrics) , the targets and all related information.
In the conclusion of the report please discuss potential areas for improvement that might be identified through this particular benchmarking exercise.
Word Count: 1,200 words
In: Operations Management
We want to design and develop a text statistical analyzer. This analyzer should consider an input text as a finite sequence of characters, including letters (‘a’, .., ‘z’, ‘A’, ..., ‘Z’), digits (0,…, 9), special characters (space and new line), and punctuation characters (‘.’, ‘,’, ‘;’). The input text must end with ‘#’. A word is defined as a sequence of alphanumeric characters (letters and numbers, 0 to 9) delimited by two separators. A separator is any non-alphanumeric character. Use your favorite object-oriented programming language to write a program that accepts a text as input and gives as output the following results: i. The total number of alphanumeric characters (i.e., letters and digits). ii. The total number of letters and their frequency with respect to the total number of alphanumeric characters. 2 iii. The total number of digits and their frequency with respect to the total number of alphanumeric characters. iv. The total number of words. v. The total number of words starting with “ted” and their frequency with respect to the total number of words. vi. The total number of words ending with “ted” and their frequency with respect to the total number of words. vii. The total number of words having “ted” in the middle and their frequency with respect to the total number of words. viii. The total number of sequences of “ted” and their frequency with respect to the total number of sequences of three alphanumeric characters. The implementation of this text statistical analyzer should be as structured as possible.
Write a code for the above problem either in c/c++.
In: Computer Science
1.Generally, the main purpose of a company's public relations effort is to
Multiple Choice
get positive coverage by the press.
educate customers about their product.
establish itself as a "thought leader" in the field.
encourage word-of-mouth pass-along.
2. Which of the following statements is true about Facebook?
Multiple Choice
It is especially effective among younger adults than older adults.
It provides a free online photo and video sharing service geared to mobile phones.
It is growing faster than all other social media platforms.
It allows registered users to send out short messages called tweets.
It uses an algorithm to decide what content is placed in the user's newsfeed.
3.Solar City wants to build awareness of its residential roof-top solar cells. It needs to explain to consumers how their product is installed, how it works, and how customers pay for it. Which of the following media types should it select to achieve these goals?
Multiple Choice
educational web pages
branded apps
Instagram page
e-mail newsletters
reminder advertising
4. When a company is able to get its customers to quickly spread a message far and wide, it is referred to as
Multiple Choice
viral promotion.
search engine optimization.
a high bounce rate
a branded service.
owned media.
In: Operations Management
Write the following questions as queries in Relational Algebra. Use only the operators discussed in class (select, project, Cartesian product, join, union, intersection, set difference and renaming —in particular, no outer joins or aggregations). Type your answers. If you can’t find Greek letters in your word processor, you can simply write out the operator, all uppercase (i.e. ’SELECT’). Please use renaming consistently, as indicated in the handout. Before starting, make sure you understand the schema of the database. If you are in doubt about it, please ask the instructor.
Assume a database with schema:
ACTOR(name,age,address,nationality)
MOVIE(title,year,genre,budget,director-name,studio)
APPEARS(name,title,salary)
Write the following questions in Relational Algebra:
1. List the titles and budgets of movies where actor Keanu Reeves appeared in 1999.
2. List the names and ages of actors who have appeared in a comedy produced by Studio ’MGM’.
3. List the names of directors who have directed a drama or a comedy.
4. List the names of directors who have directed a drama and a comedy.
5. List pairs of actors who have appeared together in a movie (note: the schema of your answer should be (name1,name2), where both name1 and name2 are names of actors.
In: Computer Science
|
IN JAVA Inheritance Using super in the constructor Using super in the method Method overriding |
Write an application with five classes Vehicle, Car, Bus, Truck, and Tester class with main method in it.
The following characteristics should be used: make, weight, height, length, maxSpeed, numberDoors, maxPassenges, isConvertable, numberSeats, maxWeightLoad, and numberAxels.
Characteristics that are applicable to all vehicles should be instance variables in the Vehicle class. The others should be in the class(s) where they belong.
Class vehicle should have constructor that initializes all its data. Classes Car, Bus, and Truck will have constructors which will reuse their parents constructor and provide additional code for initializing their specific data.
Class Vehicle should have toString method that returns string representtion of all vehicle data. Classes Car, Bus, and Truck will override inherited toString method from class vehicle in order to provide appropriate string representation of all data for their classes which includes inherited data from Vehicle class and their own data.
Class Tester will instantiate 1-2 objects from those four classes (at least five total) and it will display the information about those objects by invoking their toString methods.
Submit one word document with code for all five classes, picture of program run from blueJ, and picture of UML diagram.
In: Computer Science