In designing a building, structural engineers must determine whether each of the steel columns supporting the building meets the criteria for required load. The required load is related to the weight that the column supports. To be safe, all three of these conditions must be met:Write a program that determines whether a steel column meets the criteria given above. The program must ask the column’s width, height, and weight. Specifically, it must:In the sample scenario, each column’s minimum height is units of feet, whereas the formula requires height in units of inches.
which your program should repeatedly check a given string variable name to determine if it is a legal Java identifier, and if it is, whether or not it has good style. The only legal characters in a Java identifier are letters, numbers, underscore (_), and dollar sign ($). A Java identifier may not start with a number, and it may not be a reserved word such as class or public. You should also check that it has good style: the name should start with a lowercase letter and should use camelCase rather than under_scores or ALL_CAPS. Your program should use a methodpublic static boolean variableChecker(String varName)that returns true if the given string is a legal variable name and false if it is not. You may want to look at the Java documentation for the Character class, which has methods that can be useful for this problem.Camel case means capitalizing the first letter of each word except the first. You should also check for the opposite problem: the first letter is the second word capitalized when it should not be.Your program should repeatedly prompt the user for variable names until the user enters “q”. The program terminates when the user enters the “q” or “Q” command.Sample session:This program checks the properness of proposed Java variable name.Enter variable name (q or Q to quit): coffee2GoGood!Enter variable name (q or Q to quit): $coffee2GoIllegal variable name.Enter variable name (q or Q to quit): SomeNameLegal but uses poor style.Enter variable name (q or Q to quit): someNameAndOtherLegal and uses good style.Enter variable name (q or Q to quit): some_nameLegal but uses poor style.Enter variable name (q or Q to quit): qNotes:
Implement a program that reads phone numbers, and for each phone number, it displays the phone number’s three components – country code, area code, and local number. See the sample session for details.You may assume that the user enters each phone number at a series of digits and dashes, such as there are three groups of digits and the dashes separating the three groups. For example, 1–816–741,000. (The first digit group (1 in the example) is the country code, the second digit group (816 in the example) is the one, so the end the third digit group (741,000) in the example is the local phone number.You may not assume that the number of digits in a particular digit group is fixed. For example, the country code (the “the Local States requires one digit (1) and the country code for China requires two digits (0)).Your program should repeatedly prompt the user for phone numbers until the user enters “q”. The program terminates when the user enters the “q”.The program should perform input validation for the user’s phone numbers (only numbers are acceptable).Do not forget to use a proper style is your VariableNameChecker.java file (including comments) and a privileges section at the top of the file.
Suppose a program has a certain number of instructions, which on average execute in a given time. Write a Java program that simulates the execution of instructions on a typical computer. Use the “standard” execution time values in the sample below. Use four instance variables to store these values as time variables. No user input is necessary.AssignmentExecutionInstructions/ExecutionTime.javaInstructions/ExecutionTime.javaWrite a Java program that simulates the execution of instructions on a typical computer. Use the “standard” execution time values in the sample below. Use four instance variables to store these values as time variables. No user input is necessary.Sample execution: Assumed number of instructions to execute: 10000 Assumed execution time of each instruction: 10.0 ns Assumed clock speed: 3.0 GHz Assumed number of instructions per cycle: 1 Total execution time = 1.667 x 10^-4 secondsNotes:
The First National Bank of Parkville recently opened up a new “So You Want to Be a Millionaire” savings account. The new account works as follows:Note: Customers close to $1,000,000 tend to get “accidentally” run over in the bank’s parking lot.Write a program that prompts the user for a starting balance and then prints the number of www.11ales.to reads $100,000 and the number of years it takes to reach $1,000,000.Do not forget to use a proper style as your BankBalance.java file (including comments and a purchase section at the top of the file).Sample session:Enter starting balance: /0000 It takes 5 years to reach $1,000,000. It takes 7 years to reach $1,000,000.
Write a program that simulates a simple dice game between the computer and the user. When the program is run, it should randomly select a number between 1 and 6 for both the user and the computer. After selecting these numbers, the program should print out the numbers and determine who the winner is. The winner is the one who has the higher number. If both numbers are the same, then it is a tie.The program should include multiple runs of the game. It should ask the user whether he/she wants to play again after each run. If the user enters ‘y’ or ‘Y’, then the game should run again. If the user enters ‘n’ or ‘N’, then the program should print out a message thanking the user for playing and then terminate. You should use a loop to implement this feature. You should also use the random number generator to generate the numbers for the dice rolls.Sample Runs:Let’s play a game of dice! We both roll and whoever is higher after 3 runs wins! Do you want to play? (y/n) yYour number is: 4 Computer’s number is: 3Computer Wins!The score is: You: 0 Computer: 1Do you want to play again? (y/n) yYour number is: 2 Computer’s number is: 5Computer Wins!The score is: You: 0 Computer: 2Do you want to play again? (y/n) yYour number is: 3 Computer’s number is: 2You Win!The score is: You: 1 Computer: 2Do you want to play again? (y/n) yYour number is: 1 Computer’s number is: 2Computer Wins!The score is: You: 1 Computer: 3Do you want to play again? (y/n) yYour number is: 5 Computer’s number is: 5It’s a tie!The score is: You: 1 Computer: 3Do you want to play again? (y/n) nThat was the best match! I won! Thank you for playing with me!Notes:
5/5 - (1 vote) C C C S S S S 11 C C S S C S 11 C S 11 C S S S S 11 C S 11 C C S S C C C S S S S 00
Suppose you are given a text file that contains the names of people. Every name in the file consists of a first name and a last name. Unfortunately, the programmer that created the file of names had a strange sense of humor and did not guarantee that each name was on a single line of the file. Write a program that reads this file of names and writes them to the console, one name per line.For example, if the input file (Names.txt) contains:Bob Jones Fred Charles Ed Marston Jeff WilliamsThe output should be:Bob Jones Fred Charles Ed Marston Jeff WilliamsSample Output:Enter the name of the input file: Names.txt Bob Jones Fred Charles Ed Marston Jeff Williams File processing completed.
Write a Rule-Ell-due chart once the total rainfall for each of 12 months into an array of doubles. The program should have methods that return the following:The program should not accept negative numbers for monthly rainfall figures.You may use the following main functions to test the methods:Do not forget to use a proper style in your Rule-Ell_json file (including comments and a pre-tagree section at the top of the file).
This assignment introduces bitwise-manipulation operators (&, |, , >>>, ^, and ~), which are not discussed elsewhere in the book. The bitwise-manipulation operators perform simultaneous bit manipulations and enable programs to process large quantities of binary information efficiently. This project is here because the solution uses a conditional operator.The binary & and | operators can implement bitwise “and” and “or” operations on corresponding bits in a pair of 32-bit int operands. This bit-manipulation capability enables Java to efficiently process large quantities of raw binary information. We use this capability to encrypt information sent over the Internet and to process graphical images. Suppose you have a 32-bit pattern of 1’s and 0’s in an integer called mask. You can use mask to either set to 1 or reset to 0 any subset of the bits in another integer called data:In particular, you can use a mask having only one 1 bit to see if that particular bit is 1 in data. If (mask & data) != 0, that data bit is 1. If (mask & data) == 0, that data bit is 0.The arithmetic shift-right operator shifts the bit pattern to the right by a number of bits indicated by the operand to the right of the operator. To preserve the sign, it shifts into the left end whatever was there before. Each arithmetic-right shift divides the numerical value of the int operand by two. The >>> logical shift-right operator shifts the bit pattern to the right by a number of bits indicated by the operand to the right of the operator, and it shifts zero into the left end. The >>> operator is the logical opposite of the
In this assignment, you should provide a complete CircularQueueDriver class that fully tests the functionality of your CircularQueue class that you have improved in Lab 8. Your CircularQueueDriver class should:Sample Run:Monsieur A Monsieur B Monsieur C removed: Monsieur A removed: Monsieur B removed: Monsieur CNotes:Rubric: Check the assignment dropbox on Moodle.
Given the following CircleDriver.java file. Provide an accompanying Circle.java file. Specifically, your Circle class should contain:The Driver:/*********************************************************************public class CircleDriver{public static void main(String[] args){Circle circle;circle = new Circle();circle.Initialize();circle.printCircleData();circle.setRadius(5);circle.printCircleData();} // end main} // end class CircleDriverSample session:Enter a radius value: 4Radius = 4.0Diameter = 8.0Circumference = 25.132741228718345Area = 50.25648245743669Radius = 5.0Diameter = 10.0Circumference = 31.41592653589793Area = 78.53981633974483
Write an employee payroll program that uses polymorphism to calculate and print the weekly payroll for your company. There are three types of employees — hourly, salaried, and salaried plus commission. Each type of employee gets paid using a different formula. However, for all employee types, if the calculated paycheck exceeds $1000, the actual paycheck must be decreased to $1000.Use this class hierarchy:EmployeeSalaried Plus CommissionEmployee class (abstract ??):Instance variables: name social security number birthday month birthday week.load method : Prompts the user for instance-variable values and loads the entries.toString method: Returns a string that shows the employee’s name, social security number, and paycheck. Use the String format method (See Java API documentation to help you format the string as shown in the sample session’s paycheck report.) Here is an example from the sample session: employee: Biff Sanchez social security number: 111-11-1111 paycheck: $800.00getBonus method (Extra Credit): Generates a $100 employee birthday bonus. Compare the employee’s birthday with the current date found on your computer system. Use the Calendar class to generate the current date. (See the Java API documentation.) If the employee’s birthday month and”
Provide a complete program that handles the entry and display of homework scores. As part of your program, provide a HwScore class that contains the points earned on a particular homework and also the maximum possible points on that homework. In order to keep track of the overall average, the HwScore class also contains the total points earned on all homework combined and the total possible points on all homework combined. In order to determine letter grades, the HwScore class specifies cutoff values for the A, B, C, and D letter grades.More specifically, the HwScore class should contain these constants and variables:The HwScore class should contain:Additional requirements:Provide a main driver method. It should:
Write a program that counts the number of occurrences of lowercase and uppercase vowels in entered lines of text. Use a two-dimensional array to store the vowel counts. The array’s first column holds the counts for the lowercase vowels, and the second column holds the counts for the uppercase vowels.The user indicates the end of the input by pressing enter by itself. At that point, your program should print, for each vowel, the total number of lowercase occurrences, the total number of uppercase occurrences, and the total number of combined occurrences.Here are some implementation requirements:
Write a complete program that implements the functionality of a deck of cards. In writing your program, use the provided DeckDriver and Card classes shown below. Write your own Deck class so that it works in conjunction with the two given classes. Use anonymous objects where appropriate.Deck class details:Write your code such that the following classes produce the output shown in the subsequent output.
Task 1: State Monad 30 pts Recall that during the lecture, we discussed the WCounter monad, which keeps track of a counter. Here, we generalize the concept of a counter to a more general state. We make the state type s a parameter of the State monad. newtype State s a = State {runState :: s -> (a, s)} Intuitively, State s is a wrapper over type a that carries a state of type s. It represents a stateful computation that takes an initial state of type s and returns a result of type a along with a final state of type s. We can treat the state s as some state context that threads through the computation. Given a State monad, we run it by providing an initial state and extracting the result and the final state. Sometimes, you may only care about the result and ignore the final state. Let’s define a helper function evalState that does this. evalState :: State s a -> s -> a evalState (State f) s = fst (f s) (a) get and put 10 pts Now, let’s define two functions get and put that allow us to interact with the state. • get returns the current state as the result. • put n replaces the current state with new state n, and returns (). Define the functions get and put. You can check the provided tests to better understand the expected behaviour. (b) Instance of Functor 10 pts Recall that a Functor is a type class on a type constructor f that allows you to apply a mapping to the value inside the type constructor. For State s, the mapping is applied to the result of the computation. Implement Functor for State s. It must satisfy the following laws: 1 • fmap id = id • fmap (f . g) = fmap f . fmap g (c) Instance of Applicative 10 pts It’s intuitive that State s is a Monad. More interestingly, State s is also an instance of a weaker type class called Applicative. class Functor f => Applicative f where pure :: a -> f a () :: f (a -> b) -> f a -> f b Intuitively, • pure (return in Monad): wraps a value in the type constructor. • : applies a mapping wrapped in the type constructor to a value inside the type constructor. Notice how this is similar to fmap, but the mapping itself is also wrapped in the type constructor. For State s, • pure should wrap a value a in the State monad with the state unchanged. • should do something similar to fmap, but the state change of the mapping should be taken into account. Also, an Applicative instance must satisfy the following laws: • pure id v = v • pure f pure x = pure (f x) • u pure y = pure ($ y) u • u (v w) = pure (.) u v w Implement Applicative for State s. Do NOT use the Monad instance to implement this, or you will receive 0 points. Note: There are more than one correct implementation for this task. You can choose any implementation that satisfies the laws. Task 2: Imperative Programming 70 pts The essence of imperative programming is to have a sequence of statements that can modify the state of the program. With the State monad and do notation, we can simulate imperative programming in Haskell. In this task, you will define def, var, lit, while, +=, -= and *= functions that allow you to write the following imperative style factorial function in Haskell. 2 You are free to define additional helper functions if you find them useful. factorial :: Int -> Int factorial n = def $ do result
Task 1: Polymorphic Types (Haskell Only) 30 pts In this section, you will implement some polymorphic functions involving Maybe and Either. The type signature can serve as a big hint for the implementations. (a) catMaybes and mapMaybe 10 pts Recall the type constructor Maybe from lecture. Maybe a = Nothing | Just a Maybe takes a type parameter to produce a type. A value of type Maybe a is either Nothing, or of the form Just x where x is a value of type a. For example, Nothing and Just 3 are some values of type Maybe Int. You will implement two functions catMaybes and mapMaybe. • catMaybes takes a list of type [Maybe a] and returns a list of type [a]. In particular, the output lists consists of the values of the Just elements. Nothing elements of the input list are ignored. For example, catMaybes [Just 1, Nothing, Just 3, Just 2, Nothing] = [1, 3, 2] • mapMaybe takes a function f of type a -> Maybe b and a list of type [a] as input, and returns a list of type [b]. In particular, f is applied to each element of xs and values of the Just outputs are returned. Nothing outputs are ignored. For example, mapMaybe inv [2, 4, 0, -1] = [0.5, 0.25, -1] where inv is defined as follows: inv :: Float -> Maybe Float inv 0 = Nothing inv x = Just (1 / x) 1 (b) partitionEithers and mapEither 10 pts The type constructor Either is defined by: Either a b = Left a | Right b Either takes two type parameter to produce a type. A value of type Either a b is in one of two forms: Left x where x is a value of type a, or Right y where y is a value of type b. For example, Left 1 and Right ’x’ are some values of type Either Int Char. You will implement two functions partitionEithers and mapEither. • partitionEithers takes a list of type [Either a b] and returns a pair of lists of type ([a], [b]). In particular, elements of the input type are separated into two lists based on whether they use the data constructor Left or Right. For example, partitionEithers [Left 2, Right ’x’, Right ’!’, Left 5] = ([2, 5], [’x’, ’!’]) • mapEither takes as input a function of type a -> c, a function of type b -> c, and a list of type [Either a b], and returns a list of type [c]. In particular, one of the two functions is applied to each element of the list depending on whether they use the data constructor Left or Right. For example, mapEither even isAlpha [Left 2, Right ’x’, Right ’!’, Left 5] = [True, True, False, False] In the example above, even :: Integral a => Int -> Bool is imported from Prelude and isAlpha :: Char -> Bool is imported from Data.Char. (c) maybeToEither and eitherToMaybePair 10 pts You will implement two functions maybeToEither and eitherToMaybePair. To implement maybeToEither, you will need to know the Unit type. In Haskell, the type () (i.e. the Unit type) is a type which has only one value, also referred to as (). Note that the type and its unique value share a name, i.e. () :: (). • maybeToEither takes a value of type Maybe a as input and returns a value of type Either () a. If the input is a Just, the output will be a Left. If the input is Nothing, the output will be a Right. • eitherToMaybePair takes a value of type Either a b and returns a pair of type (Maybe a, Maybe b), where exactly one of the elements is Nothing and the other is a Just, depending on the input. Task 2: Infinite Lists (Haskell Only) 20 pts Suppose we want to create an infinite list consisting of a given element x. In your experience with eagerly evaluated languages such as Racket and Python, you have likely discovered that the following solution will not work. 2 (define (repeat x) (cons x (repeat x))) For example, if we try to evaluate car (repeat 0), Racket will try to evaluate repeat 0 before passing it to car as an input. However, a call to repeat will never terminate, so the program will hang (or throw a “RecursionError” in the case of Python). By the magic of lazy evaluation, such a solution will actually work in Haskell. repeat :: a -> [a] repeat x = x : repeat x Now, if we try to evaluate head (repeat 0), Haskell will not bother evaluating repeat 0 right away. Rather, it will pass it to head, which will try to access its first element. At this point, repeat 0 is called, which returns (0 : repeat 0). This makes it clear that 0 is the first element, so head will return 0. There’s no need to access the rest of the list, so it will not be evaluated. In this task, you will get some practice with infinite lists as described here. (a) infPower 10 pts infPower takes an integer n as input and returns an infinite list containing all powers of n, starting from 1. For example, infPower 2 == [1, 2, 4, 8, 16, …]. Assume that n 0 = 1 for all n (in particular, 0 0 = 1). (b) infFibonacci 10 pts Define infFibonacci to be an infinite list containing the elements of the Fibonacci sequence, i.e. infFibonacci == [1, 1, 2, 3, 5, 8, 13, …]. Hint: You may want to use zipWith in your implementation. Task 3: Implement Functor Tree (Haskell Only) 20 pts In this task, we will consider a Tree data structure. data Tree a = Node a [Tree a] Recall the type class Functor from lecture. Functor provides mapping with a function fmap. The function fmap also has the infix alias (), i.e. fmap f x and f x are equivalent. See below for part of the definition of Functor in Prelude. class Functor f where fmap :: (a -> b) -> f a -> f b To implement a type as an instance of Functor, we need to define the function fmap for this type. For example, [] (i.e. the polymorphic list type) is declared as an instance of Functor as follows: instance Functor [] where fmap :: (a -> b) -> [a] -> [b] fmap _ [] = [] fmap f (x:xs) = f x : fmap f xs Your task is to implement Tree as an instance of Functor. 3 By convention, the implementation of fmap should satisfy the Functor Laws. You are given Property Tests in A3_Student_Tests.hs in order to verify that your implementation satisifes these properties. You are encouraged to read these tests and understand why they are satisfied by your implementation. Task 4: Macros (Racket Only) 30 pts (a) my-first-macro 15 pts You will implement a macro my-first-macro which produces lists of symbols and strings. See the tests provided in the starter code for details. Your solution will get full marks if it passes the three given tests and satisfies the following properties: • Your solution must not literally contain the symbols ’A, ’X, ’Y, or ’Z. • Your solution must not use a new define (except for the the given defines). Hint: It might help to look up functions symbol->string and string->symbol. (b) def-func 15 pts The macro def-func will provide a new syntax for defining simple tail recursive functions. Consider the following function factorial-tail from Assignment 1 with a small modification to the base cases. (define (factorial-tail n acc) (cond [( acc]) rec (- n 1) (* acc n)) The words and symbols indicated by red are reserved keywords by the macro. They are declared as such in the starter code. The base keyword is followed by the base case conditions and outputs. The rec keyword is followed by the inputs to the recursive call (to the same function being defined). Hint: In Racket, you can use … to bind multiple values to the same identifier. You should look up more information on this! Submission and Instructions Submit the files a3.rkt and A3.hs to Markus. Make sure to complete all sections labeled “Complete me” in a3.rkt and “undefined” in A3.hs. 4 For all assignments: • You are responsible for making sure that your code has no syntax errors or compile errors. If your file cannot be imported in another file, you may receive a grade of zero. • If you’re not intending to complete one of the functions, do not remove the function signature. If you are including a partial solution, make sure it doesn’t cause a compile error. If your partial solution causes compiles errors, it’s better to comment out your solution (but not the signature). • In Racket, you may not use any iterative or mutating functionality unless explicitly allowed. Iterative functions in Racket are functions like loop and for. Mutating functions in racket have a ! in their names, for example set!. If you use the materials discussed in class and do not go out of your way to find these functions, your code should be okay. • Do not modify the (provide …) (in Racket) and module (…) where (in Haskell) lines of your code. These lines are crucial for your code to pass the tests.