Part A: Simple array algorithms
public class Numbers { /** Computes the number of even and odd values in a given array @param values an array of integer values @return an array of length 2 whose 0 entry contains the count of even elements and whose 1 entry contains the count of odd values */ public static int[] evenOdds(int[] values) { // your work here } } Code tester public class NumbersTester { public static void main(String[] args) { int[] a = { 1, 2, 3 }; int[] r = Numbers.evenOdds(a); System.out.println(r[0] + " " + r[1]); System.out.println("Expected: 1 2"); a[1] = 5; r = Numbers.evenOdds(a); System.out.println(r[0] + " " + r[1]); System.out.println("Expected: 0 3"); a = new int[0]; r = Numbers.evenOdds(a); System.out.println(r[0] + " " + r[1]); System.out.println("Expected: 0 0"); } }
Part B: Removing duplicates
Step1
A common typo is to accidentally duplicate a word, which can be be
rather embarrassing.
Your task is to design and implement a program that removes adjacent duplicates. This class reads a file and puts all words into an ArrayList<String> called words. Your task is to complete the method removeAdjacentDuplicates to remove all adjacent duplicates in words. Develop a plan and write pseudocode for this task. Questions to think about. Scribe: How do you plan to find duplicates? Scribe: What will you do when you find them? Pay special attention to what happens at the beginning or end of the array list. Show your lab instructor your pseudocode before doing the next step.
Step 2
Implement your solution. To test, download and unzip this zip file.
Copy each file into the same directory that
contains your BlueJ project. Do not copy the folder.
You must unzip the zip file. Don't simply move the zip into your BlueJ directory.
Make a Text object on the BlueJ workbench. Right-click and call pick. Pick the file typo.txt. Right-click and call removeAdjacentDuplicates (i.e. your method). Right-click and call explore. . Scribe: Is the duplicate “be” removed?
Step 3
Run this tester. Scribe: Did you pass all tests? If not, what did
you do to fix your code?
Driver: In your lab report, paste the correct solution.
Step 4
Now suppose you want to remove all duplicates, whether
adjacent or not. The result will be a list of unique words. For
example, if the array list contains these immortal words
Mary had a little lamb little lamb little lamb Mary had a little lamb whose fleece was white as snow And everywhere that Mary went Mary went Mary went And everywhere that Mary went the lamb was sure to go
you should produce the array list
Mary had a little lamb whose fleece was white as snow And everywhere that went the sure to go
Decide upon an algorithm and write down the pseudocode.
Scribe: Ask yourselves:
Step 5
When you are satisfied that you can implement it, add a method
removeAllDuplicates to the Text class.
Implement the method and test it as described above.
Step 6
Run this tester.
Scribe: Did you pass all tests? If not, what did you do to fix your code
Step 7
Driver: In your lab report, paste the correct solution.
Part C: Swapping
Step 1
Run this program.
The code on lines 20 to 24 is intended to swap neighboring elements. For example,
1 4 9 16 25 36
is supposed to turn into
4 1 16 9 36 25
But as you can see, it doesn't work. Now launch the BlueJ debugger. Put a breakpoint at line 20. Click Step. And then keep clicking Step and observe the program behavior until you can tell why it fails to swap the values.
Tip: To see the contents of the array, double-click on it in the Local Variables pane.
Step 2
Discuss what you learned from observing the program
How you can fix your program so that the swapping actually works? Scribe: What did you decide?
Step 3
Implement your fix and test it.
Driver: Put the fixed code in your lab report.
Part D: More Swapping with Pictures
Step 1
Unzip this file and open the project in BlueJ. Run the program.
Look at the main method in the Lab11D class. Note that a VisualArrayList is exactly like an ArrayList, except it shows you in slow motion what goes on inside.
Step 2
Come up with an algorithm for the following task. We want to swap
the first half and the second half of the array list.
For example, A B C D E F should turn into D E F A B C.
You should assume that the array list has an even number of elements (not necessarily 6).
One solution is to keep removing the element at index 0 and adding it to the back.
A B C D E F B C D E F A C D E F A B D E F A B C
Write pseudocode for this algorithm.
Ask yourselves:
Step 5
Implement your pseudocode and run it.
Driver: Paste the main method into your lab report. Watch how much faster it runs. (This should be pretty obvious since the movement of the array elements takes time.)
Add four more letters and run the program again to double-check that it works with any even number of letters.
Step 3
Implement your pseudocode and run it.
Driver: Paste the main method into your lab report.
Step 4
When you run the code, you will find that there is a lot of
movement in the array. Each call to remove(0) causes n - 1
elements to move, where n is the length of the array. If
n is 100, then you move 99 elements 50 times, (almost 5000
move operations). That's an inefficient way of swapping the first
and second halves.
Come up with a better way in which you swap the elements directly. Hint: How do you swap elements at index1 and index2?
A B C D E F D B C A E F D E C A B F D E F A B C
Write pseudocode for this algorithm.
Ask yourselves (Scribe: record the answers):
In: Computer Science
Code with Java and no imports, Method sandwiched returns true if num is in the element before and after an element that is not equal to num
sandwiched([4,5,4,6,7,3], 4) returns true
sandwiched([2,1,2], 2) returns true
sandwiched([3,3,3], 3) returns false
sandwiched([4,5,6,4], 4) returns false
sandwiched([1,1,2,3,1,4,1], 1) returns true
@param nums Integer ArrayList
@param num integer
@return true if a single number is between elements equal to num
*/ public static boolean sandwiched(ArrayList nums, int num) {
return false;
}//end sandwiched
In: Computer Science
write a java program that implements the splay tree data structure for the dictionary abstract data type.
Initially the program reads data from "in.dat", and establishes an ordinary binary search tree by inserting the data into the tree. The data file contains integers, one per line.
in.dat file contents:
3456
5678
1234
2369
7721
3354
1321
4946
3210
8765
Then the program starts an interactive mode. The commands are as follows.
S 1000 - splay the tree at 1000
F 2000 - search/find the node with key 2000
I 3000 - insert a node with key 3000
D 4000 - delete the node with key 4000
For each command,
1. Report appropriate message after the command is executed. Examples are:
Splay is done
Search is successful
Search is unsuccessful
The key is inserted into the tree
Duplicated keys
The key is deleted from the tree
The key is not in the tree
2. Display the new tree.
In: Computer Science
if this is considered more than one question then please explain part 'I' (Find the exact value of f100, f500, and f1000, where fn is the nth Fibonacci number. What are times taken to find out the exact values?). I am having trouble writing the code for both recursive and iterative functions without having the program crash. I assume its because the numbers are so high.
Goal: The main goal of the project is to let students use their prior knowledge, try to use the skills they have learnt to solve real world problems. Using this assignment students are required to learn to use the skills they have learned in their previous classes, solve the problems given and reflect on how they can apply it to solve real world issues.
Deliverables: The students are required to submit a written report along with the programs they have written. The report has to address all the prompts mentioned under the Report section. The report should be properly presented by using appropriate font (Time new roman, preferably font size 12 till 16) and grammar.
Tasks:
Report: Your report should address all of the below mentioned questions
In: Computer Science
Java, no imports:
Given a list of integers, round each number to the nearest
multiple of 5.
2.5-7.49 round to 5. 7.5-12.49
round to 10. 12.5-17.49 round to 15. etc.
return the sum of all the rounded
elements.
Implement this way for
credit:
Fill in the method directly below
this one called helperRound() to round each number.
Call that method and use the sum of the returned values.
helperSum({4.3, 16.7}) returns
20
helperSum({25.7, 21.2, 27.5})
returns 75
helperSum({2.5}) returns 5
helperSum({2.49, 12.49, 40.2,
42.5}) returns 95
@param nums is an arrayList of
doubles
@return the sum of the all elements
after rounding
*/
public static int helperSum(ArrayList<Double> nums)
{
int sum = 0;
return sum;
}//end helperSum
/**
Method HelperRound will round
a number up or down to the nearest 5.
@param num double
number,
@return the rounded up or
down number to the nearest multiple of 5.
*/
public static int helperRound(double n){
int rounded =
0;
return
rounded;
}//end HelperRound
In: Computer Science
Write a brief summary describing any encryption method not already mentioned in this chapter. In this summary, describe the history and origin of the algorithm. Compare the efficacy of this algorithm to other well-known algorithms.
Blowfish, AES, RC4, Serpent,3DES, RSA, PGP, Hashing, SHA, MD5 These are the method are already mentioned in this chapter.
In: Computer Science
Write a program that uses a while statement to read integers from the user and prints the sum, average, and largest of these numbers. The input should terminate if the user enters 999. The average should be output with 4 digits of precision.
c++ please ASAP
In: Computer Science
You have been hired as an IT expert by a small firm to set up an office for 20 staff members, half of whom will work with desktop computers and the remaining with laptop computers using wireless networks. The office would use one networked laser printer, accessible from both the desktop and laptop computers. The desktop computers will use a wired network, while the laptop computers will employ a wireless network to print and access the internet.
Start making a list of computer hardware (desktop and laptop computers), peripherals, and network components (wired and wireless) with specifications required to set up the proposed business office. Include the following:
In: Computer Science
Write a JavaScript program that computes the average of a collection of numbers and then outputs the total number of values that are greater than the average.
An A grade is any score that is at least 20% greater than the average. The B grade is any score that is not an A, but is at least 10% greater than the average. An F grade is any score that is at least 20% less than the average. The D grade is any score that is not an F, but is at least 10% less than the average. Any scores that are within 10% of the average (either less than or greater than) are C's.
In: Computer Science
How to write Prim's Algorithm with min-Heap and adjacency Lists?
In: Computer Science
A successful attack to the Internet DNS would be devastating. Explain what type of attacks can be made towards DNS. Why, to-date, such attacks in practice have not been successful? In your answer, you should consider caching in particular. Why such technique has not only proven to provide better performance, which is its original goal, but also protection against security attacks.
In: Computer Science
In your own words, describe NAT and the concept of a firewall and how they work.
In: Computer Science
How can I write a separate java class to change a given name or a given email of a user that was already saved (keep in mind that there may be numerous names and emails).
Note: It should change the name or email that is saved in the txt file.
Here is my code so far:
User class
public class User { private String fName; private String lName; private String UserEmail; public User(String firstName, String lastName, String email) { this.fName = firstName; this.lName = lastName; this.UserEmail = email; } //constructor used to load saved profile information. public User(String firstName, String lastName, String email, double income, double expenses) { this.fName = firstName; this.lName = lastName; this.UserEmail = email; } public String getFirstName() { return fName; } public String getLastName() { return lName; } public String getEmail() { return UserEmail; } public void setFirstName(String firstName) { this.fName = firstName; } public void setLastName(String lastName) { this.lName = lastName; } public void setEmail(String email) { this.UserEmail = email; } @Override public String toString() { return fName + ", " + lName + ", " + UserEmail ; } }
Profile Create class
import java.io.IOException; import java.util.Scanner; public class ProfileCreate { public User createUser() throws IOException { String fName; String lName; String email; Scanner read = new Scanner(System.in); System.out.println("First name?"); fName = read.nextLine(); System.out.println("Last name?"); lName = read.nextLine(); System.out.println("Email?"); email = read.nextLine(); System.out.println("Successful Profile Creation"); User x = new User(fName, lName, email); return x; } }
Save User Info class
import java.util.Scanner; import java.io.*; import java.util.*; import java.lang.*; public class SaveUserInfo { public void writeDetails(String saveDetails) throws IOException{ FileWriter out = new FileWriter("Details.txt"); for (int x = 0; x < saveDetails.length(); x++){ out.write(saveDetails.charAt(x)); } out.close(); } }
In: Computer Science
Meant to be written in Java JDK 14.0
1. Define 2 double variables. (a) Use method(s) define in Math class to check if these 2 numbers are equal or not. Display result (b) Write code (not to use Math method) check if these 2 numbers are equal or not. Display result
In: Computer Science
Q3. Write a program that simulates the 4-digit lock of the suitcase. You need to represent each rotatable cyllinder with some variable, it's value is going to reflect what state your lock is in. Digits are going to be 0 through 9. If 9 rotates further, it gets back to 0. If 0 rotates back, it becomes 9, just like in real suitcase locks.
Then, pick a combination that opens your suitcase, like:
1-9-9-5
And pick a random starting combination.
Write functions to allow you to change 1 digit at a time. After each change, you should check whether all 4 digits match the opening combo. If they do, show the last state and stop program, if they do not, just display the state of your lock and allow user to keep rotating.
In: Computer Science