What are the di±erences of handling Socket and ServerSocket ?
dynamically allocate array size of 10 integer check for the duplicate etntry, if present print the no of duplicate entry for every element remove the duplicate entry and reallocate the memory
State whether each of the following is true or false. If false, explain why.
a). An algorithm is a procedure for solving a problem in terms of the actions to execute and the order in which these actions execute.
b). A set of statements contained within a pair of parentheses is called a block.
c). A selection statement specifies that an action is to be repeated while some condition remains true.
d). A nested control statement appears in the body of another control statement.
e). Java provides the arithmetic compound assignment operators +=, -=, *=, /= and %= for abbreviating assignment expressions.
f). The primitive types (boolean, char, byte, short, int, long, float and double) are portable across only Windows platforms.
g).Specifying the order in which statements (actions) execute in a program is called program control.
h). The unary cast operator (double) creates a temporary integer copy of its operand.
i). Instance variables of type boolean are given the value TRue by default.
j).Pseudocode helps a programmer think out a program before attempting to write it in a programming language.
Write four different Java statements that each add 1 to integer variable x.
State whether each of the following is true or false. If false, explain why.
a). An algorithm is a procedure for solving a problem in terms of the actions to execute and the order in which these actions execute.
b). A set of statements contained within a pair of parentheses is called a block.
c). A selection statement specifies that an action is to be repeated while some condition remains true.
d). A nested control statement appears in the body of another control statement.
e). Java provides the arithmetic compound assignment operators +=, -=, *=, /= and %= for abbreviating assignment expressions.
f). The primitive types (boolean, char, byte, short, int, long, float and double) are portable across only Windows platforms.
g).Specifying the order in which statements (actions) execute in a program is called program control.
h). The unary cast operator (double) creates a temporary integer copy of its operand.
i). Instance variables of type boolean are given the value TRue by default.
j).Pseudocode helps a programmer think out a program before attempting to write it in a programming language.
Write four different Java statements that each add 1 to integer variable x.
Why is method main declared static
Write a Java program that determines a student’s grade.
The program will read three types of scores(quiz, mid-term, and final scores) and determine the grade based on the following rules:
-if the average score >=90% =>grade=A
-if the average score >= 70% and <90% => grade=B
-if the average score>=50% and <70% =>grade=C
-if the average score<50% =>grade=F
Sort the sequence 40 6 18 20 99 5 21 43 3 by hand with (you may upload you hand writing sketches): write the time complexity for each sorting method.
1. Insertion sort
2. Selection sort
3. Quicksort (picking the last element as the pivot)
4. Quicksort (using the median-of-three pivot)
5. Heap sort
Write a program that finds the area of circle, triangle, rectangle and square using a function named area. In this program, use a single class named shape which contains various implementations of ‘area’. Your program should dynamically decide which shapes area has to be found depending on the type of input. The radius is float value, base and height of triangle are double values, sides of rectangle and square are integer values.
Create a class named RecipeFactory
–public static Recipe makeRecipe(String recipeType, String recipeName, HashMap<String, String> ingredients)
–public static final String (DESSERT and DINNER)
•“Dessert” and “Dinner”
•Create an abstract class named Recipe
–protected HashMap<String, String> ingredients
•Key: ingredient name
•Value: ingredient amount
–protected String name
–HashMap<String, String> getIngredients()
•Can implement here
–String getName()
•Can implement here
•Extend your abstract class in DessertRecipe and DinnerRecipe
–Override toString
•Expected output on next slide
•Use the StringBuilder class
–Implement a constructor that takes String name and HashMap<String, String> ingredients.
–Warning: assigning HashMap from constructor and memory
•new HashMap<String, String>(hashMap)
•Hint: use “\n” to add a newline and “\t” to add the tab
–Since this will go through an autograder, you must exactly match the formatting (do not add an extra \n to the very end)
•Create a singleton class named Cookbook
–private Cookbook() { }
–private ArrayList<Recipe> recipes
–void addRecipe(Recipe r)
–void clearRecipes()
–ArrayList<Recipe> getRecipes()
Cookbook getInstance()
•Create a file named FinalProject.java
•This file will contain all your code when you submit your completed project
•Both methods you work on this week should be in the public FinalProjectclass
•You can add main to this class to help you test your code
•Define a static method that can take a recipe string and convert it into the appropriate Recipe subclass object
–Remember:
•Key: ingredient name
•Value: ingredient amount
–Use your RecipeFactory to create the object
•Practice with this test string and verify your method works.
–String test = "Cake~Dessert~flour@3/4 cups#milk@3/4 cups#sugar@2/3 cups#vanilla@1 tsp";
•Define a static method called connectToServer that takes as input a String ipAddress and integer port.
•Create a socket (client) and connect to the input port
–This will be your connection to the RecipeServer
•Make sure your socket is continually reading in messages from the socket
–Pseudocode: while (input from socket != null) print out input from socket
–Use an InputStreamReader wrapped in a BufferedReader
•If you run the following code from main in FinalProject.java…
–FinalProject.connectToServer(8765);
–System.out.println(“Hello”);
•…Hello will never be printed
•Why?
•Update your connectToServermethod
•Instead of performing the connection and reading on the main thread, perform the work in a new thread.
•To test your changes, run your code from the previous slide. Hello should be print out immediately.
•Also make sure you are still able to receive messages continually from RecipeServer.
•Write the following methods in your FinalProject class.
•public static void serializeRecipe(Recipe r, String path)
–Serialize the given recipe to the path
–You should only store the following fields
•name
•ingredients
–You can serialize the object as a Recipe
•No need to check which child class was input
•public static Recipe deserializeRecipe(String path)
–Deserialize a Recipe at the given path
•Cast the returned object to Recipe
–Note: Even if you treat it as a Recipe, the data from the child class you used will be saved
•i.e. no need to cast it to the child class
•Place all your classes in your FinalProject.java file.
•Test to make sure all the code still works.
•Submit it online to the autograder.
•Here is how testing will be done:
–A server socket will await your connection
–Recipe strings will be sent over the socket
–Your Cookbook should be updated after each recipe
–All of your methods will be tested, including your RecipeFactory and Cookbook methods.
Feel free to choose you own recipe names and ingredients.
•As you receive recipes over the socket, you should create the appropriate Recipe object (using the factory) and add them to the Cookbook list.
•Test your code with the RecipeServercode. You can use 8765 as your testing port.
–Note: You will have to run the server and your code separately. You will need to test it locally.
•You should be able to continually send messages from the server and receive them from your client.
–Do not change the server code – this version will be used to test your program.
create java code forr class called realtor commission. fields include the sale price of a house, the house, the sales commission rate and the commission. create two constructors. each constructor requires the sales prices(expressed as double) and the commission rate. one constructor requires the commission rate to be double, such as 0.06. the other requires sale price and commission rate expressed as whole number such as 6. each constructor calculates the commission value based on the price of the house multiplied by the commission rate. the difference is that the constructor that accepts the whole number must convert it into a percentage by dividing by 100. also include display function for the fields contained in realtor commission class. write a main function that instantiate at least 2 realtor commission objects one that uses decimal and one that uses a whole number as commission rate. display the realtor commission object values.