InPython,
Create a list of strings, don’t ask from the user, and return a list with the strings in sorted order, except group all the strings that begin with 'x' first.
e.g. ['mix', 'xyz', 'apple', 'xanadu', 'aardvark'] yields
['xanadu', 'xyz', 'aardvark', 'apple', 'mix']
# Hint: this can be done by making 2 lists and sorting each of them before combining them.
In: Computer Science
IN C++!!
Exercise #2: Design and implement class Radio to represent a radio object. The class defines the following attributes (variables) and methods:
Assume that the station and volume settings range from 1 to 10.
The radio station is X and the volume level is Y. Where X and Y are the values of variables station and volume. If the radio is off, the message is: The radio is off.
Now design and implement a test program to create a default radio object and test all class methods on the object in random order. Print the object after each method call and use meaningful label for each method call as shown in the following sample run.
Sample run:
Turn radio on:
The radio station is 1 and the volume level is 1.
Turn volume up by 3:
The radio station is 1 and the volume level is 4.
Move station up by 5:
The radio station is 6 and the volume level is 4.
Turn volume down by 1:
The radio station is 6 and the volume level is 3.
Move station up by 3:
The radio station is 9 and the volume level is 3.
Turn radio off.
The radio is off.
Turn volume up by 2: The radio is off.
Turn station down by 2: The radio is off.
In: Computer Science
Functions
In: Computer Science
Using python!!!!
1. Copy the file web.py from class (or class notes) into your working folder.
2. Include the following imports at the top of your module (hopefully this is sufficient):
from web import LinkCollector # make sure you did 1
from html.parser import HTMLParser
from urllib.request import urlopen
from urllib.parse import urljoin
from urllib.error import URLError
3. Implement a class ImageCollector. This will be similar to the LinkCollector, given a string containing the html for a web page, it collects and is able to supply the (absolute) urls of the images on that web page. They should be collected in a set that can be retrieved with the method getImages (order of images will vary). Sample usage:
>>> ic =
ImageCollector('http://www2.warnerbros.com/spacejam/movie/jam.htm')
>>> ic.feed(
urlopen('http://www2.warnerbros.com/spacejam/movie/jam.htm').read().decode())
>>> ic.getImages()
{'http://www2.warnerbros.com/spacejam/movie/img/p-sitemap.gif', …,
'http://www2.warnerbros.com/spacejam/movie/img/p-jamcentral.gif'}
>>> ic = ImageCollector('http://www.kli.org/')
>>> ic.feed(
urlopen('http://www.kli.org/').read().decode())
>>> ic.getImages()
{'http://www.kli.org/wp-content/uploads/2014/03/KLIbutton.gif',
'http://www.kli.org/wp-content/uploads/2014/03/KLIlogo.gif'}
4. Implement a class ImageCrawler that will inherit from the Crawler developed in amd will both crawl links and collect images. This is very easy by inheriting from and extending the Crawler class. You will need to collect images in a set. Hint: what does it mean to extend? Implementation details:
a. You must inherit from Crawler. Make sure that the module web.py is in your working folder and make sure that you import Crawler from the web module.
b. __init__ - extend’s Crawler’s __init__ by adding an set attribute that will be used to store images
c. Crawl – extends Crawler’s crawl by creating an image collector, opening the url and then collecting any images from the url in the set of images being stored. I recommend that you collect the images before you call the Crawler’s crawl method.
d. getImages – returns the set of images collected
>>> c = ImageCrawler()
>>>
c.crawl('http://www2.warnerbros.com/spacejam/movie/jam.htm',1,True)
>>> c.getImages()
{'http://www2.warnerbros.com/spacejam/movie/img/p-lunartunes.gif',
…
'http://www2.warnerbros.com/spacejam/movie/cmp/pressbox/img/r-blue.gif'}
>>> c = ImageCrawler()
>>> c.crawl('http://www.pmichaud.com/toast/',1,True)
>>> c.getImages()
{'http://www.pmichaud.com/toast/toast-6a.gif',
'http://www.pmichaud.com/toast/toast-2c.gif',
'http://www.pmichaud.com/toast/toast-4c.gif',
'http://www.pmichaud.com/toast/toast-6c.gif',
'http://www.pmichaud.com/toast/ptart-1c.gif',
'http://www.pmichaud.com/toast/toast-7b.gif',
'http://www.pmichaud.com/toast/krnbo24.gif',
'http://www.pmichaud.com/toast/toast-1b.gif',
'http://www.pmichaud.com/toast/toast-3c.gif',
'http://www.pmichaud.com/toast/toast-5c.gif',
'http://www.pmichaud.com/toast/toast-8a.gif'}
5. Implement a function scrapeImages: Given a url, a filename, a depth, and Boolean (relativeOnly)., this function starts at url, crawls to depth, collects images, and then writes an html document containing the images to filename. This is not hard, use the ImageCrawler from the prior step. For example:
>>> scrapeImages('http://www2.warnerbros.com/spacejam/
movie/jam.htm','jam.html',1,True)
>>> open('jam.html').read().count('img')
62
>>>
scrapeImages('http://www.pmichaud.com/toast/',
'toast.html',1,True)
>>> open('toast.html').read().count('img')
11
link to web.py https://www.dropbox.com/s/obiyi7lnwc3rw0d/web.py?dl=0
In: Computer Science
Concepts: Classes, Objects, Inheritance, Method overriding (toString), polymorphic methods, Array of objects, loops
1. (Classes and Objects) Hand-write a complete Java class that can be used to create a Car object as described below.
a. A Vehicle has-a: i. Registration number ii. Owner name iii. Price iv. Year manufactured
b. Add all instance variables
c. The class must have getters and setters for all instance variables
d. The class must have two constructors, a no-args and a constructor that receives input parameters for each instance variable.
2. (Inheritance) Hand-write two Java classes, Car and Truck. They are both subclasses of Vehicle.
a. A car has an additional instance variable, number of doors.
b. A truck has an additional instance variable, number of axles
c. Write a constructor that requires input for the instance variables of each class (including registration number and owner name).
d. Write getters and setters for all instance variables for both classes.
3. (Array of objects) The array of Vehicles below, write the static method, findMin which returns the lowest price of any Vehicle in the Array Vehicle [] myVehicles = new Vehicle[10]; public static double findMin(Vehicle [] theVehicles) { }
4.Write a class, TestVehicle, which does the following: 1. Creates one Car object from information entered by the user. 2. Creates one Truck object from information entered by the user. 3. Creates an Array of Vehicles that can hold 10 objects. 4. It creates up to 10 Vehicles objects from information entered by the user 5. Prints the information about each object in the format shown below using the toString methods of the classes
*****PLEASE USE THE CODE BELOW I HAVE SO FAR TO COMPLETE THE PROGRAM****
****************************************************************************************
public class Vehicle {
//Instance Variables
private int
registrationNumber;
private String ownerName;
private double price;
private int yearManufactured;
//Constructors
public Vehicle (){
this.registrationNumber=0;
this.ownerName=
" ";
this.price=0;
this.yearManufactured=0;
}
public Vehicle (int
newRegisteration, String newName, double newPrice, int newMnf)
{
this.registrationNumber = newRegisteration;
this.ownerName =
newName;
this.price =
newPrice;
this.yearManufactured = newMnf;
}
//Get methods
public int getRegistration ()
{
return
this.registrationNumber;
}
public String getName () {
return
this.ownerName;
}
public double getPrice () {
return
this.price;
}
public int getYearManufactured () {
return
this.yearManufactured;
}
//Mutators or
Setters
public void
setRegistration (int newRegistration) {
this.registrationNumber=newRegistration;
}
public void setName (String newName) {
this.ownerName=newName;
}
public void
setPrice (double newPrice) {
this.price=newPrice;
}
public void setYearManufactured (int newYearManufactured) {
this.yearManufactured=newYearManufactured;
}
public String toString () {
return "
Vehicle: \nRegistration Number: " +registrationNumber
+ " Owner Name: "+ownerName+
" Price: "+price+ " Year Manufactured: " +yearManufactured;
}
}//end of class
**********************************************************************************************
public class Car extends Vehicle {
//Instance Variables
private int numOfDoors;
//Constructors
public Car() {
super();
}
}//end of class
In: Computer Science
Take a single example and elaborate the replacement algorithms?
In: Computer Science
(SQL Coding)
Create a READ ONLY view called: VIEW_EMP_SAL_INFO
Calculate the following: The minimum, average, maximum, and sum of all salaries, and a count to show the records used. Have the calculations grouped by department name. (You will need to join with the departments table.) Round the functions to two decimal points. Give the read only constraint the name of vw_emp_sal_info_readonly.
In: Computer Science
c++
Write a program that opens a file, reads records into a
container of data structures, and prints out the sum in
order.
You can use Vectors to contain the data structures.
1. Create a data structure to represent the record ==>
struct Cost in cost.h
2. Write a function called ==> parse_account,
that parses one (string) record from a file, and
populates a data structure with that data.
3. Write a function called sum_accounts, that when
passed a container of structures, returns
a double representing the sum of the amounts in those
structures.
4. Create a main program
a) Create an appropriate container of data
structures
b) Open the accounts file
(costfile.txt)
c) While you can read a line from the file
without error
Call
parse_account, which returns a data structure.
Add the
returned data structure to the container (using, pushToV)
d) Call sum_accounts to determine the amount of
money represented
e) Print a message and the result.
Try this with some sample data, such as the following lines in
costfile.dat
You may add or create your own data file to test the program
with:
You can start with the data in a text file and then write the data
in a binary file, this
way you practice with both file types.
Description
Amount Item number
Pass_Go
200.00 135
Reading_RR 50.00
136
Connecticut 120.00
137
Chance
25.00 138
- Use input file to read the above Data
- Use header file for the data structure definition
- Use Multiple Source Files for function definitions and
Application, each in separate file (.cpp file)
In: Computer Science
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