In: Computer Science
(10) public double smallerRoot() throws Exception
Depending on the equation ax^2 + bx + c = 0:
if no roots, throw exception
if single root, return it
if two roots, return the smaller root
if infinite root, return -Double.MAX_VALUE
(11) public double largerRoot() throws Exception
if no roots, throw exception
if single root, return it
if two roots, return the larger root
if infinite root, return Double.MAX_VALUE
(12) equals method
This should OVERRIDE equals method from Object class
return true if two expressions have same a, same b and same c
(13) clone
return a copy of the calling object
(14) use javadoc style comments for the class, and the methods
At minimum, include the author, parameters and return types for each method.
(15) use javadoc to generate document for your class
(16) test your class:
you can write your own main to test your code;
but you have to pass the test in QuadraticExpressionTest.java
(17) submit
a. QuadraticExpression.java
b. QuadraticExpression.html
on blackboard.In: Computer Science
In: Computer Science
What makes functional values possible in Haskell?
In: Computer Science
1).Modify the project so that records are inserted into the random access file in ascending order using an insertion sort methodology with the Social Security Number acting as the key value. This requires defining the method compareTo() in the Personal and Student classes to be used in a modified method add() in Database. The method finds a proper position for a record d, moves all the records in the file to make room for d, and writes d into the file.
2. With the new organization of the data files. find() and modify() must also be modified. Both methods should now stop their sequential search when they encounter a record greater than the record looked for, or they reach the end of the file.
I am having trouble initializing the compareTo()
Personal.java
import java.io.*;
import java.util.Arrays;
import Student.Student;
public class Personal extends IOmethods implements DbObject
{
protected final int nameLen = 10, cityLen = 10;
protected String SSN, name, city;
protected int year;
protected long salary;
protected final int size = 9*2 + nameLen*2 + cityLen*2 + 4 +
8;
Personal() {
}
Personal(String ssn, String n, String c, int y, long s) {
SSN = ssn; name = n; city = c; year = y; salary = s;
}
public int size() {
return size;
}
public void writeToFile(RandomAccessFile out) throws IOException
{
writeString(SSN,out);
writeString(name,out);
writeString(city,out);
out.writeInt(year);
out.writeLong(salary);
}
public void writeLegibly() {
System.out.print("SSN = " + SSN + ", name = " + name.trim()
+ ", city = " + city.trim() + ", year = " + year
+ ", salary = " + salary);
}
public void readFromFile(RandomAccessFile in) throws IOException
{
SSN = readString(9,in);
name = readString(nameLen,in);
city = readString(cityLen,in);
year = in.readInt();
salary = in.readLong();
}
public void readKey() throws IOException {
System.out.print("Enter SSN: ");
SSN = readLine();
}
public void readFromConsole() throws IOException {
System.out.print("Enter SSN: ");
SSN = readLine();
System.out.print("Name: ");
name = readLine();
for (int i = name.length(); i < nameLen; i++)
name += ' ';
System.out.print("City: ");
city = readLine();
for (int i = city.length(); i < cityLen; i++)
city += ' ';
System.out.print("Birthyear: ");
year = Integer.valueOf(readLine().trim()).intValue();
System.out.print("Salary: ");
salary = Long.valueOf(readLine().trim()).longValue();
}
public void copy(DbObject[] d) {
d[0] = new Personal(SSN,name,city,year,salary);
}
public int compareTo(String p) {
}
}
Student.java
import java.io.*;
public class Student extends Personal {
public int size() {
return super.size() + majorLen*2;
}
protected String major;
protected final int majorLen = 10;
Student() {
super();
}
Student(String ssn, String n, String c, int y, long s, String m)
{
super(ssn,n,c,y,s);
major = m;
}
public void writeToFile(RandomAccessFile out) throws IOException
{
super.writeToFile(out);
writeString(major,out);
}
public void readFromFile(RandomAccessFile in) throws IOException
{
super.readFromFile(in);
major = readString(majorLen,in);
}
public void readFromConsole() throws IOException {
super.readFromConsole();
System.out.print("Enter major: ");
major = readLine();
for (int i = major.length(); i < nameLen; i++)
major += ' ';
}
public void writeLegibly() {
super.writeLegibly();
System.out.print(", major = " + major.trim());
}
public void copy(DbObject[] d) {
d[0] = new Student(SSN,name,city,year,salary,major);
}
public int compareTo() {
}
}
DbObject.java
import java.io.*;
public interface DbObject {
public void writeToFile(RandomAccessFile out) throws
IOException;
public void readFromFile(RandomAccessFile in) throws
IOException;
public void readFromConsole() throws IOException;
public void writeLegibly() throws IOException;
public void readKey() throws IOException;
public void copy(DbObject[] db);
public int size();
}
IOmethods,java
import java.io.*;
public class IOmethods {
public void writeString(String s, RandomAccessFile out) throws
IOException {
for (int i = 0; i < s.length(); i++)
out.writeChar(s.charAt(i));
}
public String readString(int len, RandomAccessFile in) throws
IOException {
String s = "";
for (int i = 0; i < len; i++)
s += in.readChar();
return s;
}
public String readLine() throws IOException {
int ch;
String s = "";
while (true) {
ch = System.in.read();
if (ch == -1 || (char)ch == '\n') // end of file or end of
line;
break;
else if ((char)ch != '\r') // ignore carriage return;
s = s + (char)ch;
}
return s;
}
}
Database.java
import java.io.*;
public class Database {
private RandomAccessFile database;
private String fName = new String();;
private IOmethods io = new IOmethods();
Database() throws IOException {
System.out.print("File name: ");
fName = io.readLine();
}
private void add(DbObject d) throws IOException {
database = new RandomAccessFile(fName,"rw");
database.seek(database.length());
d.writeToFile(database);
database.close();
}
private void modify(DbObject d) throws IOException {
DbObject[] tmp = new DbObject[1];
d.copy(tmp);
database = new RandomAccessFile(fName,"rw");
while (database.getFilePointer() < database.length()) {
tmp[0].readFromFile(database);
if (tmp[0].equals(d)) {
tmp[0].readFromConsole();
database.seek(database.getFilePointer()-d.size());
tmp[0].writeToFile(database);
database.close();
return;
}
}
database.close();
System.out.println("The record to be modified is not in the
database");
}
private boolean find(DbObject d) throws IOException {
DbObject[] tmp = new DbObject[1];
d.copy(tmp);
database = new RandomAccessFile(fName,"r");
while (database.getFilePointer() < database.length()) {
tmp[0].readFromFile(database);
if (tmp[0].equals(d)) {
database.close();
return true;
}
}
database.close();
return false;
}
private void printDb(DbObject d) throws IOException {
database = new RandomAccessFile(fName,"r");
while (database.getFilePointer() < database.length()) {
d.readFromFile(database);
d.writeLegibly();
System.out.println();
}
database.close();
}
public void run(DbObject rec) throws IOException {
String option;
System.out.println("1. Add 2. Find 3. Modify a record; 4.
Exit");
System.out.print("Enter an option: ");
option = io.readLine();
while (true) {
if (option.charAt(0) == '1') {
rec.readFromConsole();
add(rec);
}
else if (option.charAt(0) == '2') {
rec.readKey();
System.out.print("The record is ");
if (find(rec) == false)
System.out.print("not ");
System.out.println("in the database");
}
else if (option.charAt(0) == '3') {
rec.readKey();
modify(rec);
}
else if (option.charAt(0) != '4')
System.out.println("Wrong option");
else return;
printDb(rec);
System.out.print("Enter an option: ");
option = io.readLine();
}
}
}
UseDatabase.java
import java.io.IOException;
public class UseDatabase {
static public void main(String a[]) throws IOException {
// (new Database()).run(new Personal());
(new Database()).run(new Student());
}
}
In: Computer Science
One Problem/Question (Four Parts)
1 Sample run:
0
10
20
30
40
50
60
70
80
90
100
2 Sample run:
449
9
143
101
115
479
299
115
199
211
The Average of the numbers is: 212.0
3 & 4 Sample run:
say something: hello
you said : hello
say something: what's up?
you said : what's up?
say something: done
you said : done
Goodbye!
In: Computer Science
Write an iterative method countHighEarners() to return the number of nodes storing an high earner employee. Method countHighEarners()is in the class LinkedList that has private instance variable list of Node type. Class Node is private inner class inside of class LinkedList, and has public instance variables: data and next of Employee and Node type, respectively. In addition, class Node has constructor and toString method. See LinkedList class bellow on the right. Class Employee has getters for all data, method String toString() that returns string of all employee data, and method boolean isHighEarner() that returns true if employee's salary is above average, and false otherwise.
|
public int countHighEarners () { } |
public class LinkedList
{
private Node list;
public LinkedList()
{
list = null;
}
public Node getList()
{
return list;
}
. . .//other methods
// insert method countHighEarners here
private class Node
{
public Employee data;
public Node next;
public Node(Employee emp)
{
data = emp;
next = null;
}
public String toString()
{
return data.toString();
}
}
}
In: Computer Science
I NEED JAVASCRIPT PROGRAM
Chose an activity that you enjoy. It can be a sport that you
watch or participate in, collecting, hobbies, etc. Do
not use a collection of movies since we have covered that in the
assignment. Do not use the same topic as a
friend. (If two students that don’t know each other happen to
select the same topic, that is fine, since they will
naturally have different property names and values.)
1. Select some aspect of the activity and create an array of
objects denoting that aspect. For example, in
the case of movies, you could select movies themselves or you could
select movie stars, producers,
soundtracks, etc. There are many aspects of any activity that you
can use. Feel free to use the web to
research these values.
The array must contain at least 10 objects
The data must be actual data not just gibberish
Each object will have at least 4 properties with values
Each object in the array should have the same set of property names
with different values
At least one string value and one number value
Other properties can be of any JavaScript value type
2. Create a function that can be used to format your object in a
nice string format for output. This function
should be a pure function that takes one object as an argument and
return a string. The string can be
formatted in any way that you feel is appropriate for your data. It
must use one or more properties from
the object in the output string.
3. Create a new array from your array that only contains string
values produced by the above function. Sort
this array alphabetically and display an appropriate heading
followed by this list one string per line.
4. Prompt the user for a numeric value. Display a string (formatted
using the function above) for each of the
objects that have a property (you choose) that are greater or less
(up to you) the value provided by the
user. Be sure to provide a message if there are no objects that
match.
5. Calculate the average value based on some property of your
objects that makes sense. Output it with
descriptive text in a nicely formatted way.
Sample Output:
Movie Titles:
12 Angry Men (1957) by Sidney Lumet
Chak de! India (2007) by Shimit Amin
Hera Pheri (2000) by Priyadarshan
La Haine (1995) by Mathieu Kassovitz
Pulp Fiction (1994) by Quentin Tarantino
Schindler's List (1983) by Steven Spielberg
The Dark Knight (2008) by Christopher Nolan
The Godfather (1972) by Francis Ford Coppola
The Godfather: Part II (1974) by Francis Ford Coppola
The Good, the Bad and the Ugly (1966) by Sergio Leone
The Lord of the Rings: The Fellowship of the Ring (2001) by Peter
Jackson
The Lord of the Rings: The Return of the King (2003) by Peter
Jackson
The Shawshank Redemption (1994) by Frank Darabont
Movies newer than 2005:
Chak de! India (2007) by Shimit Amin
The Dark Knight (2008) by Christopher Nolan
The average age of these movies is: 31 years old.
In: Computer Science
Explain Parameter passing Semantics in Java/C++
In: Computer Science
what is Object-oriented modeling?
what is UML modeling?
What is the different between Object-oriented modelling and UML
In: Computer Science
In: Computer Science
A certain programming language allows only two-character variable names. The first character must be a letter (upper or lower case) and the second character can be a letter (upper or lower case) or a digit. How many possible variable names are there?
In: Computer Science
c++
Run the following sorting algorithms:
1. Bubble sort
2. Insertion sort
3. Quicksort
4. Mergesort
Under the following scenarios for input data:
1. Uniform random
2. Almost sorted (90% sorted – 1 in 10 is out of place)
3. Reverse sorted
On data of sizes 5,000, 10,000, … in increments of 5,000 up to …,
50,000
-Attach a screenshot of a program compilation below
-Attach a screenshot of a successful program run below
-Attach a graph (either line graph or bar graph) below.
3.2 90% Sorted Data (10% of data are out of their sorted
position)
--Attach a graph below
3.3 Reverse-Sorted (sorted in non-increasing order) Data
--Attach a graph below
template code:
//a main template file
#include <iostream>
#include <iomanip>
#include <ctime>
#include <string>
#include "Sort.h"
using namespace std;
void init_and_run(double**, int*, const int, const int, const int,
const int);
void print(double**, int*, const int, const int, const int, const
int);
int main()
{
int size[] = {100, 1000, 5000, 10000, 50000};
const int NUM_SIZES = 7;
const int NUM_SORT_ALGO = 7;
const int NUM_DATA_TYPES = 3;
const int NUM_REPEAT = 5;
double* totalSortTime[NUM_DATA_TYPES];
init_and_run(totalSortTime, size, NUM_SIZES, NUM_DATA_TYPES,
NUM_SORT_ALGO, NUM_REPEAT);
print(totalSortTime, size, NUM_SIZES, NUM_DATA_TYPES,
NUM_SORT_ALGO, NUM_REPEAT);
return 0;
}
void init_and_run(double**totalSortTime, int* size, const int
NUM_SIZES, const int NUM_DATA_TYPES, const int NUM_SORT_ALGO, const
int NUM_REPEAT)
{
clock_t time;
for(int d = 0; d < NUM_DATA_TYPES; d++) {
totalSortTime[d] = new double [NUM_SIZES * NUM_SORT_ALGO];
for(int i = 0; i < NUM_SIZES * NUM_SORT_ALGO; i++)
totalSortTime[d][i] = 0;
}
for(int d = 0; d < NUM_DATA_TYPES; d++) { //data types
for(int s = 0; s < NUM_SIZES; s++) { //input sizes
for(int r = 0; r < NUM_REPEAT; r++) { //repetitions
Sort st(size[s], d);
for(int t = 0; t < NUM_SORT_ALGO; t++) { //sort algorithms
st.setSortType(t);
time = clock();
st.run();
//st.print();
time = clock() - time;
totalSortTime[d][s * NUM_SORT_ALGO + t] += time;
}
}
}
}
for(int d = 0; d < NUM_DATA_TYPES; d++) {
delete [] totalSortTime[d];
}
}
void print(double**totalSortTime, int* size, const int NUM_SIZES,
const int NUM_DATA_TYPES, const int NUM_SORT_ALGO, const int
NUM_REPEAT)
{
//cout << string(25, '=') << " AVG SORTING TIME "
<< string(25, '=') << endl;
cout << "\nEach value displayed below shows the average
sorting time with five instances.\n";
cout << "The values may vary depending on the system and the
implementation.\n";
cout << "The relative performances, however, should be the
same.\n";
cout << "So are the performances of those algorithms as N or
% of sorted numbers grows." << endl;
string dataType[NUM_DATA_TYPES] = {
" RANDOM_TBL_SIZE ",
// " TBL_SIZE_PARTIAL_SORT_25 ",
// " TBL_SIZE_PARTIAL_SORT_50 ",
// " TBL_SIZE_PARTIAL_SORT_75 ",
" TBL_SIZE_PARTIAL_SORT_95 ",
" REVERSE_SORTED "
};
string sortAlgo[NUM_SORT_ALGO] = {
"INSERTION",
"MERGESORT",
"QUICKSORT_L",
"QUICKSORT_R",
"COUNTING"
"BUBBLE",
"SELECTION"
};
cout << fixed << setprecision(2) << endl;
for(int d = 0; d < NUM_DATA_TYPES; d++) {
cout << string(25, '-') << left << setw(26)
<< dataType[d] << string(25, '-') << endl;
cout << right << setw(7) << "N";
for(int a = 0; a < NUM_SORT_ALGO; a++)
cout << right << setw(14) << sortAlgo[a];
cout << endl;
for(int s = 0; s < NUM_SIZES; s++) {
cout << right << setw(7) << size[s];
for(int t = 0; t < NUM_SORT_ALGO; t++)
cout << right << setw(14) << totalSortTime[d][s *
NUM_SORT_ALGO + t]/NUM_REPEAT;
cout << endl;
}
cout << endl;
}
}
#ifndef DATAGEN_H
#define DATAGEN_H
class DataGen {
private:
int modType;
int sortType;
int dataType;
int** data;
int inputSize;
int arraySize;
int partialSortSize;
void getRandom();
void copy();
void partialSort();
public:
DataGen();
DataGen(int*[], int, int, int);
void generateData();
//void partialSort();
};
#endif
#include "DataGen.h"
class Sort
{
friend class DataGen;
private:
int* numbers[5];
int algo_types;
int size;
DataGen *dg;
void (Sort::*fp) ();
void insertionSort();
void bubbleSort();
void selectionSort();
void mergeSort();
void quickSort_L();
void quickSort_R();
void countingSort();
void partition_L(int, int, int&, int&);
void partition_R(int, int, int&, int&);
void merge(int, int, int[]);
void recursive_mergeSort(int, int, int[]);
void recursive_quickSort_L(int, int);
void recursive_quickSort_R(int, int);
void clear();
public:
Sort(int, int);
~Sort();
void setSortType(int);
void run();
void print();
};
#endif
In: Computer Science
Write a two paragraph description of hashing that a non-technical user could understand .
In: Computer Science
JAVA
Write a program that demonstrates how various exceptions are caught with
catch (Exception exception)
This time, define classes ExceptionA (which inherits from class Exception) and ExceptionB (which inherits from class ExceptionA). In your program, create try blocks that throw exceptions of types ExceptionA, ExceptionB, NullPointerException and IOException. All exceptions should be caught with catch blocks specifying type Exception.
In: Computer Science