Write a JAVA program that reads a text file.
Text file contains three lines.
Place each line in an array bucket location.
Add to the end of the array list.
Print the array.
In: Computer Science
Part 1:
Write a program that has 2 classes. The first class is a test class with 2 methods; a main method, and a static method called drinkMilk(). The drinkMilk method returns void, and will throw an exception of type OutOfMilkException. The purpose of this exercise is to demonstrate that a method can get information back to the caller through an exceptions object that is thrown inside the called method and caught in the calling method. (OutOfMilkException is the second class that you will create.)
The drinkMilk method will: save what time you start drinking your milk, here’s how:
In the System class there is a method:
public static long currentTimeMillis()
Returns the current time in milliseconds.
Then, in an infinite loop, while (true) generate random integers between 0 – 1000 and perform an int division using the random integer as the denominator. Use anything except 0 as the numerator. You must use the Random class to generate random integers, not the Math class. Eventually you will execute a divide by zero.
Each time you generate a random number print out “Gulp.” Use print instead of println. When a division by zero exception is thrown, the drinkMilk method will catch the ArithmeticException exception. Within the catch block throw an exception of type OutOfMilkException.
(You are catching one kind of exception, creating a more descriptive exception that describes the situation, and throwing that exception.)
The outOfMilkException object will contain a value that indicates how long it took to drink the milk – in milliseconds, i.e., how long it took to generate a 0 value. The main method that catches the OutOfMilkException will printout the number of milliseconds that it took to drink the milk.
This demonstrates that information is "thrown" from the drinkMilk method to the main method. Keep in mind that the purpose of this exercise is to “throw” a piece of information from one method to another without using the usual mechanisms for passing information from one method to another, i.e., parameters and return values.
Note: It is not good practice to put any calculation inside an Exception object. These objects are holders of data only! No arithmetic operators, no method calling. Maybe a String concat + once in a while. The methods in the exceptions should never contribute to the solution; their role is exclusively to know about what went wrong, and to be thrown and caught.
Part 2: Assertions
Background:
The ‘assert’ keyword was added to Java in version 1.2. That caused a problem. “assert” was used for many years in C++, and when Java did not provide that mechanism, many Java programmers added a public method called ‘assert’ into their code. When Java 1.2 came out, all of the code that used ‘assert’ wouldn’t compile, because they had a method name that matched a keyword.
assert somethingTrue; // does nothing
assert somethingThatIsFalse; // this will throw an AssertionError
It became necessary to add some qualifiers to the compile and run commands in Java, so that assertions might be used at compile time and/or at run time. Since we are doing all of our work within Eclipse, you will need to figure out how to turn these on within the Eclipse environment. Eclipse is generating the commands to compile and run the java programs – that’s good, we don’t have to type the commands, but it makes it harder when we need to change what command is generated.
The compile and run switches for Assertions are quite elaborate. Assertions are only used during development. Everyone will want to turn off this feature at run-time when the product is released.
When you have figured out how to turn Assertions on / off within Eclipse, post your solution on the discussion board. If the answer is already there, but you can add something, do it. You don’t need to re-post something that is already there.
Coding exercise:
Demonstrate using the assert keyword. Create a private method that takes an int parameter.
If the value passed in is negative, the method will assert something that is false, causing the method to throw an AssertionError.
Pass the value that was passed into the method to the constructor of the AssertionError class.
All of this is done with a single assert statement.
Why did I ask you to make this method private? It works the same with public methods. Put a comment in the code if you can find an answer that question. (It isn’t something you can figure out, you will need to read about the convention and the reason for having that convention.)
In: Computer Science
PYTHON ?Write a superclass encapsulating a rectangle. A rectangle has two attributes representing the width and the height of the rectangle. It has methods returning the perimeter and the area of the rectangle. This class has a subclass, encapsulating a parallelepiped, or box. A parallelepiped has a rectangle as its base, and another attribute, its length; it has two methods that calculate and return its area and volume. You also need to include a client class (with the main method) to test these two classes.??
In: Computer Science
In: Computer Science
Compare and contrast (1) the procedural/functional approach (defining a function for each operation with the function body providing a case for each data variant) and (2) the object-oriented approach (defining a class for each data variant with the class definition providing a method for each operation). (250 own words)
In: Computer Science
Create a program in C that performs the following tasks:
In: Computer Science
In: Computer Science
Create an abstract class Employee. Your Employee class should include the following attributes:
First name (string)
Last name (string)
Employee id (string)
Employee home street address (string)
Employee home city (string)
Employee home state (string)
Write a constructor to initialize the above Employee attributes. Create an abstract method called earnings. Create another class HourlyEmployee that inherits from the abstract Employee class. HourEmployee must use the inherited parent class variables and add in attributes HourlyRate and HoursWorked. Your HourEmployee class should contain a constructor that calls the constructor from the Employee class to initialize the common instance variables but also initializes the HourlyRate and HoursWorked. Implement the Employee abstract earnings method in HourlyEmployee to calculate the earnings for a week. Note that earnings is hourly rate * hours worked.
Create a test class that prompts the user for the information for two hourly employees, creates the 2 two hourly employees objects, calls the earnings method then displays the attributes and earnings for each of the two hourly.
SUBMIT YOUR JAVA CODE AND PSUEDOCODE
In: Computer Science
8.14 LAB: Warm up: Contacts (C Programming Only)
You will be building a linked list. Make sure to keep track of both the head and tail nodes.
(1) Create three files to submit.
(2) Build the ContactNode struct per the following specifications:
Ex. of PrintContactNode() output:
Name: Roxanne Hughes Phone number: 443-555-2864
(3) In main(), prompt the user for three contacts and output the
user's input. Create three ContactNodes and use the nodes to build
a linked list. (2 pts)
Ex:
Person 1 Enter name: Roxanne Hughes Enter phone number: 443-555-2864 You entered: Roxanne Hughes, 443-555-2864 Person 2 Enter name: Juan Alberto Jr. Enter phone number: 410-555-9385 You entered: Juan Alberto Jr., 410-555-9385 Person 3 Enter name: Rachel Phillips Enter phone number: 310-555-6610 You entered: Rachel Phillips, 310-555-6610
(4) Output the linked list. (2 pts)
Ex:
CONTACT LIST Name: Roxanne Hughes Phone number: 443-555-2864 Name: Juan Alberto Jr. Phone number: 410-555-9385 Name: Rachel Phillips Phone number: 310-555-6610
In: Computer Science
Chapter 20, programming challenge 2: Linked List Sorting and Reversing
Modify the LinkedList1 class presented in this chapter by adding sort() and reverse() methods. The reverse method reverses the order of the elements in the list, and the sort method rearranges the elements in the list so they are sorted in alphabetical order. Do not use recursion to implement either of these operations. Extend the graphical interface in the LinkedListDemo class to support sort and reverse commands, and use it to test the new methods.
This must be done in java (netbeans)
In: Computer Science
foreach(int i ______ intArr)
In: Computer Science
Question 12 PYTHON:
Write a function named first_last that takes a single parameter,
string_list (a list of strings). The
function first_last should return a list of the strings in
string_list that are not empty and that begin
and end with the same letter.
For example, the following would be correct input and output for the function first_last.
response = ['to', 'that', 'I', 'say', '', 'hurrah']
print(first_last(response))
['that', 'I', 'hurrah']
Question 13 (20 points)
Write a function named number_luck. The function number_luck takes two parameters:
1. lucky, a list of lucky numbers between 2 and 12,
inclusive
2. unlucky, a list of unlucky numbers between 2 and 12,
inclusive
Every number is either lucky, unlucky or boring (neither lucky nor unlucky).
The function number_luck should
1. ask the user for a number in the range 2 to 12 (you may
assume that the user provides valid
input)
2. print a message echoing the user’s number and stating whether it
is lucky, unlucky or boring
3. return an integer that is the user’s number
For example, the following would be correct input and output.
>>> a_num = number_luck([7, 11], [2, 3, 12])
Give me a number from 2 to 12: 7
7 is lucky. You win!
>>> print(a_num)
7
In: Computer Science
Which areas of the network would a college IT staff most likely NOT have to redesign as a direct result of many students bringing their own tablets to school to access school resources? And explain the function of each design (choose 4) (1pt each correct ans. + 3 pts. for each explanation)?
Extranet:
Intranet:
Wired LAN:
Wireless LAN:
Wireless WAN:
In: Computer Science
(C++)Problem #1: Coin Tossing Simulation: Write a program that simulates coin tossing. Let the program prompt the user to enter the number of tosses N and count the number of times each side of the coin appears. Print the results. The program should call a separate function flip() that takes no arguments and returns 0 for tails and 1 for heads. The program should produce different results for each run.
Sample Input / Output
Enter the number of tosses N: 1000
The total number of Heads is: 495
The total number of Tails is: 505
In: Computer Science
PLEASE MAKE UP ANY 3 NONLINEAR PROGRAMMING PROBLEMs BY YOUR OWN AND SOLVE IT in either EXCEL, matlab or lingo.
In: Computer Science