JAVA:
Compute the average of a list of user-entered integers representing rolls of two dice. The list ends when 0 is entered. Integers must be in the range 2 to 12 (inclusive); integers outside the range don't contribute to the average. Output the average, and the number of valid and invalid integers (excluding the ending 0). If only 0 is entered, output 0. The output may be a floating-point value. Ex: If the user enters 8 12 13 0, the output is:
Average: 10 Valid: 2 Invalid: 1
Hints:
Use a while loop with expression (userInt != 0).
Read the user's input into userInt before the loop, and also at the end of the loop body.
In the loop body, use an if-else to distinguish integers in the range and integers outside the range.
For integers in the range, keep track of the sum, and number of integers. For integers outside the range, just keep track of the number.
Use a cast to get a floating-point output: (double) sum / num .
Whenever dividing, be sure to handle the case of the denominator being 0.
In: Computer Science
There are various selection structures that can be used in programming. What are the different relational operators used in selection structures in the Python programming language? Also, explain what short-circuiting is with respect to compound conditional expressions in Python. Provide examples to support your response in addition to constructive feedback on the structures and circumstances posted by your peers. Provide at least one reference to support your findings.
In: Computer Science
Write a code in python to read in a file of spice like component and/or control data. break the data up into fields in a class object.
In: Computer Science
This problem is about query flooding in P2P networks. Here, we explore the reverse-path routing of the QueryHit messages in Gnutella. Suppose that Alice issues a Query message. Furthermore, suppose that Bob receives the Query message (which may have been forwarded by several intermediate peers) and has a file that matches the query.
5.1. As we know when a peer has a matching file, it sends a QueryHit message along the reverse path of the corresponding Query message. An alternative design would be for Bob to establish a direct TCP connection with Alice and send the QueryHit message over this connection. What are the advantages and disadvantages of such an alternative design?
5.2 In the Gnutella protocol, when the peer Alice generates a Query message, it inserts a unique ID in the message’s MessageID field. When the peer Bob has a match, it generates a QueryHit message using the same MessageID as the Query message. Describe how peers can use the MessageID field and local routing tables to accomplish reverse-path routing.
5.3. An alternative approach, which does not use message identifiers, is as follows. When a query message reaches a peer, before forwarding the message, the peer augments the query message with its IP address. Describe how peers can use this mechanism to accomplish reverse-path routing.
In: Computer Science
JAVA Create an HourlyEmployee class that inherits from Employee
and has two new instance variables: hours, which represents the
hours worked, and wage, which represents the employee's pay per
hour. (Both are doubles.) Create a constructor that takes the
arguments first name, last name, social security number, hourly
wage, and the number of hours worked. Also create accessors,
mutators, an earnings method that returns the money earned by the
employee this week, and a toString method that returns information
about the employee in the form of a String. The setWage method
should ensure that the wage is nonnegative, and the setHours method
should ensure that the value of hours is between 0 and 168 (the
number of hours in a week).
Create a Driver class with a main method that prompts the user to
enter a first name, last name, social security number, hours, and
wage for an employee. Then, the program should create an
HourlyEmployee object and use its toString method to print
information about it.
SAMPLE RUN #1: java Driver
Enter·first·name:Bobbi↵ Enter·last·name:Benton↵ Enter·social·security·number:765-42-0092↵ Enter·hours·worked:53↵ Enter·wage:15.8↵ hourly·employee:·Bobbi·Benton↵ social·security·number:·765-42-0092↵ hours:·53.0·↵ wage:·15.80·↵ earnings:·837.40↵
In: Computer Science
How do I add a method to make sure that the user inputs in uppercase only and anything entered in lower case throws an error as well as to make sure when they are halving the questioned number they don't enter any decimals?
import java.util.*;
public class TestCode {
public static void main(String[] args) {
String choice = "YES";
Random random = new Random();
Scanner scanner = new Scanner(System.in);
ArrayList data = new ArrayList();
int count = 0,correct=0;
while (!choice.equals("NO")) {
int randomInt = 2 * (random.nextInt(5) + 1);
System.out.println(randomInt);
System.out.println("Enter half of the random number");
int temp=scanner.nextInt();
if(2*temp==randomInt){
correct++;
System.out.println("Correct");
}
else{
System.out.println("Incorrect");
}
data.add(randomInt);
count++;
System.out.print("Want another number (Yes / No)? ");
choice = scanner.next();
}
System.out.println("You got like "+correct+" answers and
"+(count-correct)+" incorrect answers");
int min = Collections.min(data);
int max = Collections.max(data);
System.out.print("Values are: ");
for (int i = 0; i < data.size(); i++) {
System.out.print(data.get(i) + " ");
}
System.out.println();
System.out.println("Percentage of Yes values is " + (((count -
1) * 100.0) / count));
System.out.println("Maximum value is " + max);
System.out.println("Minimum value is " + min);
}
}
In: Computer Science
In 2-3 pages, develop a Secure Application Development Procedure which addresses each of the OWASP Top Ten Vulnerabilities. Make sure to address the following:
In: Computer Science
How can I fix this code to accomplish the goal of reading and writing on binary or text files?
3 import java.io.*;
4 import java.io.FileOutputStream;
5 import java.io.FileInputStream;
6 import java.util.Scanner;
7
8 public class ReadAndWrite implements Serializable
9 {
10 public static void main(String[] args)
11 {
12 boolean file = true;
13 Scanner inputStream;
14 PrintWriter outputStream;
15 FileInputStream inputBinary;
16 FileOutputStream readBinary;
17 FileInputStreamText writeText;
18 FIleOutputStreamText readText;
19 StringBuffer contents = new StringBuffer();
20 Scanner keyboard = new Scanner(System.in);
21 String yn = "Y N", fileName = " ", line = " ", tb = "T B", rw =
"R W";
22
23 while(file)
24 {
25 System.out.println("Enter the file name.");
26 fileName = keyboard.nextLine();
27 System.out.println("Choose binary or text file (b/t):");
28 tb = keyboard.nextLine();
29 if (tb.equalsIgnoreCase("b"))
30 {
31 System.out.println("Choose read or write (r/w):");
32 rw = keyboard.nextLine();
33 if (rw.equalsIgnoreCase("r"))
34 {
35 inputBinary = new PrinterWriter(new
FileInputStream(fileName));
36 System.out.println("File contains:" + readBinary);
37 inputBinary.close();
38 }
39 else
40 {
41 System.out.println("Enter a line of information to write to the
file:");
42 line = keyboard.nextLine();
43 System.out.println("Would you like to enter another line?
(y/n)");
44 yn = keyboard.nextLine();
45 if(yn.equalsIgnoreCase("n"))
46 {
47 file = false;
48 }
49 }
50 }
51 }
52
53 do
54 {
55 System.out.println("Choose read or write (r/w):");
56 rw = keyboard.nextLine();
57 if (rw.equalsIgnoreCase("r"))
58 {
59 FileInputStreamText = new Scanner(new
FileInputStream(outputStream + ".txt"));
60 String textLine = null;
61 }
62 else
63 {
64 System.out.println("Enter a line to write to the file:");
65 line = keyboard.nextLine();
66 System.out.println("Would you like to enter another line?
(y/n)");
67 yn = keyboard.nextLine();
68 if(yn.equalsIgnoreCase("n"))
69 {
70 file = false;
71 }
72 }
73 }while(tb.equalsIgnoreCase("t"));
74 System.out.println("Process completed");
75 }
76 }
In: Computer Science
Prompt user to enter an integer number from console. Use 2 methods to multiply this number by factor 7, display result. This is required to be done in MIPS Assembly Language.
In: Computer Science
I am struggling with this java code
ublic class HW2_Q4 {
public static void main(String[] args) {
// Define the input string. Note
that I could have written it all on a
// single line, but I broke it up
to match the question.
String input = "Book , Cost ,
Number\n"
+ "Hamlet , 24.95 ,
10\n"
+ "King Lear , 18.42 ,
13\n";
// Construct a Scanner, but have it
read the String defined above,
// rather than System.in. Note that
I'm leaving the name of the
// variable as "keyboard" so that
it matches the question.
Scanner keyboard = new
Scanner(input);
// Declare and initialize variables
to store the total cost and number.
double totalCost = 0;
int totalNumber = 0;
// Write your code below using
only these variables. Your code
// should not contain literal
values. For instance, do not calculate
// the total cost with the
statement "totalCost = 24.95 + 18.42".
// Instead, use the Scanner to read
the values from the keyboard.
In: Computer Science
Python
DESCRIPTION
Write a program that will read an array of integers from a file and do the following:
●
Task 1: Revert the array in N/2 complexity time
(i.e., number of steps)
.
●
Task 2: Find the maximum and minimum element of the array.
INPUT OUTPUT
Read the array of integers from a file named “
inputHW1.txt
”. To do this, you can use code snippet
from the “
file.py
” file. This file is provided in
Canvas. Copy the “
read
” function from this file and
paste it to your program file. Then, if you call the read function like the following:
numbers = read()
# The following "read" function reads values from a file named "inputHW1.txt" and
returns the values in an array.
def read():
file = open("inputHW1.txt", "r")
line = file.readline()
values = []
for value in line.split(' '):
values.append(int(value))
return values
the integers will be read from file and loaded in the numbers array.
Now, print the reverted arra
y, minimum element and maximum element of the array.
For example, if the input array is: 1 4 9 11 8 3 2 5 10
the following will be printed on the screen:
Reverted array: 10 5 2 3 8 11 9 4 1
Maximum element: 11
Minimum element: 1
EXTRA CREDIT - 30%
Find the trend change points of the array. You will get
30% extra credit
for doing this task.
Trend
change points are those points where the array
changes its direction
and
goes from
increasing to decreasing order or decreasing to increasing order.
Note that, you have to find the trend change points of the input array, not the reverted array.
For example, if the input array is: 1 4 9 11 8 3 2 5 10
the following will be printed on the screen:
Trend change points: 11 8 5
In: Computer Science
/* Homework Lab 6
This program does not work. There are three errors that you will need to fix:
TODO: Fix the following 3 errors.
ERROR #1) The compiler knows there is something wrong, and it
will issue a
warning and and two error messages, all related to the same
problem.
Read the error and warning messages
carefully to point you in the
right direction. Did you know if
you double-click on an error
message (in the Error List) that it
will take you to the spot in
your program where the compiler
detects the problem? Also, get
in the habit of ALWAYS reading
messages carefully. You may not
understand them right now, but over
time you will come to understand
the messages better and better. One
of these three messages will be
most helpful, but it is still
informative to see that the other two
messages come as a result of this
single problem.
ERROR #2) After fixing ERROR #1 your program will run. However,
you will get
incorrect results. It will say "Congratulations!" even when not
perfect.
ERROR #3)
But when you find and fix that error, something still lurks
behind that the
compiler does not complain about and that will give incorrect
output. The output
will still display a zero after the decimal point even if it should
be .3 or .7...
IMPORTANT NOTE ON ERROR #3: You are NOT allowed to change the
data type of any constant
or variable to make this work. You also MUST divide by
NUM_OF_SCORES
when calculating the average.
*/
//------------------------------------------------------------------------------
#include <iostream>
#include <iomanip>
using namespace std;
//------------------------------------------------------------------------------
int main()
{
int score1; // To hold three test scores
int score2;
int score3;
double average; // To hold the average score
const int NUM_OF_SCORES = 3;
// Get the three test scores.
cout << "Enter 3 test scores and I will average
them: ";
cin >> score1 >> score2 >>
score3;
// Calculate and display the average score.
average = (score1 + score2 + score3) /
NUM_OF_SCORES;
cout << fixed << showpoint <<
setprecision(1);
cout << "Your average is " << average
<< endl;
// Follow up with a message.
if (average = 100);
cout << "Congratulations!
Those are all perfect scores" << endl;
else
cout << "Not perfect yet -
Better luck next time." << endl;
cout << endl;
return 0;
}
/* Sample program interactions:
------------------------------------------------------------------------------
BEFORE FIXING ERROR #1:
======================
The program will not run!
AFTER FIXING ERROR #1:
======================
Test #1:
-----------------------------------------------------------------------------
Enter 3 test scores and I will average them: 100 100 100
Your average is 100.0
Congratulations! Those are all perfect scores
Press any key to continue . . .
Test #2:
-----------------------------------------------------------------------------
Enter 3 test scores and I will average them: 70 80 90
Your average is 80.0
Congratulations! Those are all perfect scores
Press any key to continue . . .
AFTER FIXING ERROR #2:
======================
Test #1:
-----------------------------------------------------------------------------
Enter 3 test scores and I will average them: 100 100 100
Your average is 100.0
Congratulations! Those are all perfect scores
Press any key to continue . . .
Test #2:
-----------------------------------------------------------------------------
Enter 3 test scores and I will average them: 70 80 90
Your average is 80.0
Not perfect yet - Better luck next time.
Press any key to continue . . .
Test #3 (use calculator to see what this average should
be):
-----------------------------------------------------------------------------
Enter 3 test scores and I will average them: 72 80 90
Your average is 80.0
Not perfect yet - Better luck next time.
Press any key to continue . . .
AFTER FIXING ERROR #3:
======================
Test #1:
-----------------------------------------------------------------------------
Enter 3 test scores and I will average them: 100 100 100
Your average is 100.0
Congratulations! Those are all perfect scores
Press any key to continue . . .
Test #2:
-----------------------------------------------------------------------------
Enter 3 test scores and I will average them: 70 80 90
Your average is 80.0
Not perfect yet - Better luck next time.
Press any key to continue . . .
Test #3:
-----------------------------------------------------------------------------
Enter 3 test scores and I will average them: 72 80 90
Your average is 80.7
Not perfect yet - Better luck next time.
Press any key to continue . . .
*/
In: Computer Science
Create a C++ program that will prompt the user to input an positive integer number and output the corresponding number to words. Check all possible invalid input data. (Please use only switch or if-else statements. Thank you.)
In: Computer Science
Create your own data. Ensure the data will test all criteria and prove all sorts worked as required. Be sure that all tables and queries have descriptive names. “Query 1” and “Step 1” are examples of poor names.
Step 1
Build a Database named DBMS Course Project. The database should include the following tables:
Step 2
Extract the First and Last Name, Address, City, State, Zip Code and Phone Number of each Senior on the database. Sort by Last Name, then First Name (1 sort).
Step 3
Extract the First and Last Name, and GPA of each student who qualifies for the Dean’s List. Sort by GPA, then Last Name, then First Name (1 sort). A GPA of 3.25 is required to make the Dean’s List.
In: Computer Science
Write a program that asks the user for a Fahrenheit temperature and calls a function name Celsius that returns the temperature in Celsius of the Fahrenheit temperature sent to it. Your function will look similar to this: double celsius(double f)
{
double c;
:
:
return c;
}
In: Computer Science