Please write in java: Write a recursive method toNumber that forms the integer sum of all digit characters in a string. For example, the result of toNumber("3ac4") would be 7. Hint: If next is a digit character ('0' through '9'), Character.isDigit(next) is true and the numeric value of next is Character. digit(next, 10).
In: Computer Science
**********************************java**********************************************************
Purpose: The purpose for this project is to reinforce the knowledge from Chapter 9 of the textbook. The students will learn how to write a user defined class.
Project Objectives:
Apply UML design on user defined class
Write overloaded constructors of a class
Write mutators (i.e. get methods) and accessors (i.e. set methods) of a class
Write overloaded methods
Write main method to test the user defined class
Class Diagram:
Student must implement the Temperature class according to the following class design.
Temperature
-‐degree: double
-‐scale: char
+Temperature()
+Temperature(degree: double)
+Temperature(scale: char)
+Temperature(degree: double, scale: char)
+getDegreeInCelsius(): double
+getDegreeInFahrenheit(): double
+setDegree(degree: double): void
+setDegree(scale: char): void
+setDegree(degree: double, scale: char): void
+equals(obj: Temperature): boolean
+isLessThan(obj: Temperature): boolean
+isGreaterThan(obj: Temperature): boolean
Method details
The name of a method explains it usage.
getDegreeInCelsius will return the temperature’s degree in its equivalent Celsius degree. If the temperature’s scale is ‘C’, then the return value is temperature’s degree. If the temperature’s scale is ‘F’, the return value is calculated by the following formula: C = (F-‐32)*5/9. For example, if the temperature degree is 77 and scale is ‘F’, then the method will return 25since (77-‐32)*5/9 = 25. For another example, if the temperature degree is 77 and scale is ‘C’, then the method will return 77.
getDegreeInFahrenheit will return the temperature’s degree in its equivalent Fahrenheit degree. If the temperature’s scale is ‘F’, then the return value is temperature’s degree. If the temperature’s scale is ‘C’, the return value is calculated by the following formula: F = 1.8C+32. For example, if the temperature degree is 25 and scale is ‘F’, then the method will return 25; For another example, if the temperature degree is 25 and scale is ‘C’, then the method will return 77 since 1.8*25+32 = 77.
void setDegree(double degree) will reset the temperature to given degree without change the scale of the temperature.
void setDegree(char scale) will reset the temperature to given scale without change the degree of the temperature.
void setDetree(double degree, char scale) will reset the temperature to given degree of given scale.
equals method returns true if this temperature is equal to parameter Temperature; false otherwise. You need to compare tow temperatures under same scale. For example, you either compare whether getDegreeInCelsius return same value for both temperatures, or whether getDegreeInFahrenheit return the same value for both temperatures.
isLessThan method return true if this temperature is less than parameter Temperature ; false otherwise. Again, you need to compare two temperatures under same scale.
isGreaterThan method returns true if this temperature is greater than parameter Temperature; false otherwise. Again, you need to compare two temperatures under same scale.
Main method requirements
The main method must create four Temperature objects by using all four constructors.
The main method must test all set and get methods by setting the attributes according to user’s input then displaying the temperature by printing out the result of get methods.
The main function must test equals, isLessThan, and isGreaterThan methods by compare two Temperature objects and print out the result.
Sample pseudo code for main function
Create a Temperature object by calling default constructor
Print out the temperature’s degree in Celsius
Print out the temperature’s degree in Fahrenheit
Ask user to enter the scale and degree of the temperature
Call set method(s) to set the temperature’s degree and scale
Print out the temperature’s degree in Celsius
Print out the temperature’s degree in Fahrenheit
Repeat step 1 to 7 for all other three constructorsto create three more temperatures
Test if temperature1 equals temperature2 and print out appropriate message
Test if temperature1 is less than temperature2 and print out appropriate message
Test if temperature1 is greater than temperature2 and print out appropriate message
Repeat step 9 to 11 for other pairs of temperatures
Note on main function and debugging
Fix one error a time.
If step 2 output is wrong, then either constructor is wrong or getDegreeInCelsiu function is wrong
If step 3 output is wrong, then either constructor is wrong or getDegreeInFahrenheit function is wrong
If step 6 output is wrong, then either set method(s) is wrong or getDegreeInCelsiu function is wrong
If step 7 output is wrong, then either set method(s) is wrong or getDegreeInFahrenheit function is wrong
If step 9 output is wrong, then the equals method has problem
If step 10 output is wrong, then the isLessThan method has problem
If step 11 output is wrong, then the isGreaterThan method has problem
*******************************MY CODE **********************************************
I tried to make this program but I had trouble with a few things, please complete the program and make sure it compiles. If possible, comment. Thank you
import java.util.Scanner;
public class Temperature
{
public static void main(String[] args) {
Temperature temp = new Temperature(0.0,C);
System.out.println("The first Temperature has been created using
the default constructor which sets");
System.out.println("the degree to a default value of " +
this.degree + "and the scale to a default value of " +
this.scale);
System.out.println("The first Temperature is " + getDegreeInCelsius
+ " C ");
System.out.println("The first Temperature is " +
getDegreeInFahrenheit + " F ");
Scanner in = new Scanner(System.in);
System.out.print("set the degree (a number) and the scale (F or C)
of the first Temperature.");
double degree = in.nextdouble();
System.out.print("First set the degree: " + this.degree);
//The first Temperature has been created using the default
constructor which sets
//the degree to a deafault value of 0.0 and the scale to a default
value of C.
//The first Temperature is 0.00 C.
//The first Temperature is 32.00 F.
//Set the degree (a number) and the scale (F or C) of the first
Temperature.
//First set the degree:
}
Temperature();
Temperature(double degree){
this.degree = degree;
}
Temperature(char scale){
this.scale = degree;
}
Temperature(double degree, char scale){
this.degree = degree;
this.scale = degree;
}
private double degree;
private char scale;
public double getDegreeInCelsius(){
if(scale == 'C' || scale == 'c'){
return degree;
}else {
return (degree - 32) * 5 / 9.0;
}
}
public double getDegreeInFahrenheit(){
if(scale == 'F' || scale == 'f'){
return degree;
}else {
return (1.8 * (degree) + 3.2);
}
}
public void setDegree(double degree) {
this.setDegree = degree;
}
public void setDegree(char scale) {
this.setDegree = scale;
}
public void setDegree(double degree, char scale) {
this.setDegree = degree;
this.setDegree = scale;
}
public boolean equals(){
if(this.getDegreeInCelsius() == obj.getDegreeInCelsius()){
return true;
}else if(this.getDegreeInFahrenheit() ==
obj.getDegreeInFahrenheit()){ {
return false;
}
}
public boolean isLessThan(Temperature temp){
if(this.getDegreeInCelsius() < temp.getDegreeInCelsius()){
return true;
}else if(this.getDegreeInFahrenheit() <
temp.getDegreeInFahrenheit()){
return true;
}else{
return false;
}
}
public boolean isGreaterThan(Temperature temp){
if(this.getDegreeInCelsius() > temp.getDegreeInCelsius()){
return true;
}else if(this.getDegreeInFahrenheit() >
temp.getDegreeInFahrenheit()){
return true;
}else{
return false;
}
}
In: Computer Science
Python question
Define a function called hash_string_weighted_folding(string_to_hash, modulus) that takes two parameters: a string variable string_to_hash and an integer called modulus. The function returns an integer that is the hash value of string_to_hash.
This hash function processes the string in blocks of 4 characters (the final block may be shorter depending on the length of string_to_hash) and multiplies each character’s ASCII value by a weight. The weight depends on the position of the character in the block of 4 characters: the ASCII value of the character in the first position is multiplied by 256 to the power of zero (i.e., by 1); the character in the second position has its ASCII value multiplied by 256 to the power of 1 (i.e., by 256); the third character has its ASCII value multiplied by 256 to the power of 2; and the fourth character in each block has its ASCII value multiplied by 256 to the power of 3. All of the resulting weighted values are summed and the function returns the result of this sum modulus the parameter modulus.
Note: You can use the inbuilt Python ord() function to return the ASCII value of a string character.
For example:
Test | Result |
---|---|
string_to_hash = "Kea" print(hash_string_weighted_folding(string_to_hash, 5519)) |
2959 |
string_to_hash = "Piwaiwaka" print(hash_string_weighted_folding(string_to_hash, 6089)) |
5997 |
string_to_hash = "Kaki" print(hash_string_weighted_folding(string_to_hash, 3797)) |
2339 |
In: Computer Science
General Requirements:
• You should create your programs with good programming style and
form using proper blank spaces, indentation and braces to make your
code easy to read and understand;
• You should create identifiers with sensible names;
• You should make comments to describe your code segments where they are necessary for readers to understand what your code intends to achieve.
• Logical structures and statements are properly used for specific purposes.
Objectives
This assignment requires you to write a program in Java that
calculates the day of week and the week of month for a given date,
as well as to print the calendar of the month where the given date
exists. Please note, you are not allowed to use any date based
classes predefined in Java JDK.
Background
For a given date, we can use the Gregorian calendar to find the
corresponding the day of the week, and the week of the month. For
example, May 25 2019 is a Saturday and locates in the fourth week
of May 2019. In general, we can use the following formula (Zeller’s
congruence) to calculate the day of a week for any given date after
October 15 1582.
where
• h is the day of the week (0 = Saturday, 1 = Sunday, 2 = Monday,
3=Tuesday, 4=Wednesday, 5=Thursday, 6 = Friday)
• q is the day of the month
• m is the month (3 = March, 4 = April, 5 = May, 6=June, 7=July, 8=August, 9=September, 10=October, 11=November, 12=December, 13=January, 14 = February)
• K is the year of the century (year mod 100).
• J is the zero-based century (year/100). For example, the
zero-based centuries for 1995 and 2000 are 19 and 20
respectively.
For example, For 1 January 2000, the date would be treated as the
13th month of 1999, so the values would be: q=1, m=13, K=99, J=19,
so the formula is
h = (1+[13*(13+1)/5]+99+[99/4]+[19/4]+5*19) mod 7 =
(1+[182/5]+99+[99/4]+[19/4]+95) mod 7 =
(1+[36.4]+99+[24.75]+[4.75]+95) mod 7 = (1+36+99+24+4+95) mod 7 = 0
= Saturday
However, for 1 March 2000, the date is treated as the 3rd month of
2000, so the values become q=1, m=3, K=00, J=20, so the formula
is
h = (1+[13*(3+1)/5]+00+[00/4]+[20/4]+5*20] mod 7 =
(1+[10.4]+0+0+5+100) mod 7 = (1+10+5+100) mod 7 = 4 =
Wednesday
Tasks You will create one program called MyCalendar.java. You
should first implement the MyCalendar and MyDate classes from the
following UML class diagrams. You can add extra attributes and
methods, but the attributes and methods shown in the UML diagrams
can’t be modified.
MyCalendar - myDate: MyDate - day: Day + Main(String[] args) +
MyCalendar(MyDate myDate) + dayOfWeek(): Day + weekOfMonth(): int +
printCalendar(): void
• Day is an enumeration data type which contains the days of the
week, i.e., Monday, Tuesday, Wednesday, Thursday, Friday, Saturday,
and Sunday. • MyCalendar(MyDate myDate)is the construction method
of the class MyCalendar. • dayOfWeek() method will return the day
of the week for myDate. • weekOfMonth() method will return the week
of the month for myDate. • printCalendar() method will print the
calendar of the month for myDate. • Main(String[] args) method will
read a given date from the command line, the format of the given
date is dd/mm/yyyy. You can assume the date input format is always
correct, but the input date may be invalid. For example, 31/02/2017
is not a valid date. If the input date is not a valid date, the
program will ask user to input another valid date through keyboard.
This process will be repeated until a valid date is input by the
user. The valid input date will be used to create the object
(myDate) of class MyDate, then the object (myCalendar) of class
MyCalendar. The dayOfWeek() method, weekOfMonth() method, and
printCalendar() method of myCalendar object will be called to
display the day of the week, the week of the month, and the
calendar of the month for the input date by the user.
MyDate
- day: int - month: int - year: int + MyDate(int day, int month,
int year) + getDay(): int + getMonth(): int + getYear(): int +
isDateValid(): boolean
• MyDate(int day, int month, int year) is the constructor of the
class MyDate. • getDay() method returns the day of myDate object. •
getMonth() method returns the month of myDate object. • getYear()
method returns the year of myDate object. • isDateValid() method
returns a Boolean value, i.e., a true when the myDate object is
valid, and a false otherwise (notes: January, March, May, July,
August, October, December has 31 days. April, June, September,
November has 30 days. February has 29 days in a leap year, and 28
days in a common year.).
Your program must have the exact same output as the following
example. Example of the program output:
java MyCalendar 29/02/2019 29/02/2019 in not a valid date, please
re-input a valid date: 25/05/2019 25/05/2019 is a Saturday and
located in the fourth week of May 2019 The calendar of May 2019
is:
SUN MON TUE WED THU FRI SAT 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
(Note: Each column indicates a day and the column width are fixed
to 3 characters. The distance between two adjacent columns is fixed
to three characters as well.)
In: Computer Science
|
|
In: Computer Science
What is the latency experienced by a memory controller on a row buffer hit?
In: Computer Science
Explain in details Hadoop architecture. Give an example of Hadoop MapReduce example using real data
In: Computer Science
For example:
Please write a program where you input a number of cantaloupes and Python tells you the total cost.
In: Computer Science
Create a short program in Java which contains the below information:
- Has one abstract and two regular classes
- Has two interfaces (the two regular classes have to implement at least one interface
- At least one regular class must implement both interfaces
- The two classes have to extend the abstract class
- The abstract class must have a method
- At least one interface must have a method
- At least two fields in each class, this includes the superclass
- All the fields are instantiated within the constructor
In: Computer Science
(c++) Create a program that is like deli. You can enter peoples names in, and they will be placed into a stl queue. You can also choose to "serve" someone, which will choose the next person in line. (It will say "now serving" and then the persons's name.). Last, you can choose to "quit", which will "serve" the remaining people in the queue, and then the program will end.
In: Computer Science
Python question
Define a function called max_value_length(tree) that takes a Binary Tree object as a parameter and returns an integer that contains the longest character length of all the values in that tree. Use recursion.
For example, the character length of value 123 is 3,
because there are three digits. The character length of value
“Ruru” is 4, because it has four characters. Further, the
max_value_length() function would return 4 if the
only node values in the tree were 123 and “Ruru”,
because 4 is greater than 3.
Note 1: You must use
recursion to answer this question (i.e., not loops).
Note 2: Values in the tree are always treated as
strings.
For example:
Test | Result |
---|---|
binary_tree = create_tree_from_nested_list([7, [2, [14, None, None], [5, None, None]], [9, None, [144, None, None]]]) print(max_value_length(binary_tree)) |
3 |
binary_tree = create_tree_from_nested_list(["Hoiho", ["Kaki", ["Takahe", None, None], None], ["Ruru", None, ["Moa", None, ["Piwaiwaka", None, None]]]]) print(max_value_length(binary_tree)) |
9 |
binary_tree = create_tree_from_nested_list([]) print(max_value_length(binary_tree)) |
0 |
In: Computer Science
Janet Littleton,6,7,14
Frank Edbrooke,17,9,31
Robert Hovery,25,1,18
Thomas Bingham,21,8,2
Stephen Bruce,7,9,23
For each player it shows:
For example, Janet Littleton hit 6 doubles, 7 triples and 14 home runs. Frank Edbrooke hit 17 doubles, 9 triples and 31 home runs.
Your job is to determine:
In: Computer Science
In: Computer Science
In: Computer Science
Write a program that reads the messages contained in the file message_tx.txt into a 640x480 BMP file named bbc_packet.bmp. Each line in the text file should be treated as a separate message.
#include "dirtyd.h"
#include "GLOWWORM.h"
#include "MyBMP.h"
#define OUT_BMPFILENAME "HW06_PR01.bmp"
#include
#include
#define BBC_PACKET_LENGTH_BYTES (1024)
#define BBC_MESSAGE_LENGTH_BYTES ( 32) // bytes, not bits
#define BBC_CHECKSUM_BITS ( 8) // bits
#define BBC_PACKET_LENGTH_BITS (8 *
BBC_PACKET_LENGTH_BYTES)
#define BBC_MESSAGE_LENGTH_BITS (8 * BBC_MESSAGE_LENGTH_BYTES)
#define isSet(c,b) ((c) & (1 << (b)))
#define TASKPRINT (!TRUE)
#define TASK(s) if(TASKPRINT) printf("%s\n", (s))
#define WIDTH (640)
#define HEIGHT (480)
#define fileREAD "message_tx.txt"
//#define DEBUG_ON
#ifdef DEBUG_ON
#define DEBUG(s) s
#else
#define DEBUG(s)
#endif
uint8_t *bbc_encode_byte(GLOWWORM *gw, uint8_t *packet, uint8_t
c, MyBMP *bmp)
{
for (int bit = 7; bit >= 0; bit--)
{
uint8_t bitValue = isSet(c,
bit);
uint64_t hash = GLOWWORM_addBit(gw,
!!bitValue);
uint32_t chip = hash %
BBC_PACKET_LENGTH_BITS;
printf("%c[%i] = %u (chip = %u)\n",
c, bit, !!bitValue, chip);
packet[chip] = TRUE;
if (!!bitValue == 1){
MyBMP_drawPixel(bmp, chip/WIDTH, chip%WIDTH, 0, 0, 0);
// MyBMP_drawPixel(bmp,
chip%WIDTH, chip%HEIGHT, 255, 255, 255);
//
MyBMP_drawCircle(bmp, chip%WIDTH, chip%HEIGHT, 5,
// 0, 0, 0, 1);
printf("%i and
%i\n", chip/WIDTH, chip%WIDTH);
}else {
MyBMP_drawPixel(bmp, chip/WIDTH, chip%WIDTH, 255, 255, 255);
}
//packet[GLOWWORM_addBit(gw,
isSet(c,bit)) % (BBC_PACKET_LENGTH_BITS)] = TRUE;
}
return packet;
}
uint8_t *bbc_encode(uint8_t *packet, uint8_t *s, MyBMP
*bmp)
{
int byte = 0;
if (NULL == packet)
{
TASK("Create packet (1 byte per bit
for convenience)");
packet = (uint8_t *)
malloc(BBC_PACKET_LENGTH_BITS * sizeof(uint8_t));
if (!packet)
{
printf("Failed
to allocate packet -- abort!\n");
exit(EXIT_FAILURE);
}
for (int i = 0; i <
BBC_PACKET_LENGTH_BITS; i++)
packet[i] =
0;
}
TASK("Encode bookend marks");
packet[0] = TRUE;
packet[BBC_PACKET_LENGTH_BITS - 1] = TRUE;
TASK("Create and Initialize Glowworm");
GLOWWORM *gw = GLOWWORM_new();
GLOWWORM_init(gw);
TASK("Encode the message");
while ((byte < BBC_MESSAGE_LENGTH_BYTES) &&
(NUL != s[byte]))
bbc_encode_byte(gw, packet,
s[byte++], bmp);
TASK("Encode padding bytes (all zero)");
while (byte++ < BBC_MESSAGE_LENGTH_BYTES)
bbc_encode_byte(gw, packet, 0,
bmp);
TASK("Encode checksum bits");
for (int bit = 0; bit < BBC_CHECKSUM_BITS;
bit++)
packet[GLOWWORM_addBit(gw, 0) %
BBC_PACKET_LENGTH_BITS] = TRUE;
GLOWWORM_del(gw);
return packet;
}
uint32_t chipFromHash(uint64_t hash)
{
return (uint32_t) (hash %
BBC_PACKET_LENGTH_BITS);
}
void printMessagePrefix(uint8_t *message, int bits)
{
printf("MSG: ");
for (int i = 0; i < bits; i++)
printf("%c", message[i]? '1' :
'0');
printf("\n");
}
void printMessage(uint8_t *message)
{
DEBUG(printMessagePrefix(message,
BBC_MESSAGE_LENGTH_BITS);)
char ascii[BBC_MESSAGE_LENGTH_BYTES + 1];
ascii[BBC_MESSAGE_LENGTH_BYTES] = NUL;
for (int i = 0; i < BBC_MESSAGE_LENGTH_BYTES;
i++)
{
ascii[i] = 0;
for (int b = 0; b < 8;
b++)
{
ascii[i] <<= 1;
ascii[i] +=
message[8*i+b]? 1 : 0;
}
}
printf("MESSAGE: %s\n", ascii);
}
int bbc_decode(uint8_t *packet)
{
uint8_t msgBits[BBC_MESSAGE_LENGTH_BITS +
BBC_CHECKSUM_BITS];
GLOWWORM *gw = GLOWWORM_new();
GLOWWORM_init(gw);
int messages = 0;
int b = 0;
int forward = TRUE;
DEBUG(int trap = 0;)
while (b >= 0)
{
DEBUG
(
printf("DEC Bit
%03i: ", b);
printf(" %s ",
forward ? "->" : "<-");
printf(" (%02i)
", trap);
printMessagePrefix(msgBits, b);
)
// Pushing down the 0 path
if ( (forward) && (b <
BBC_MESSAGE_LENGTH_BITS + BBC_CHECKSUM_BITS) )
{
msgBits[b++] =
0;
if
(packet[chipFromHash(GLOWWORM_addBit(gw, 0))])
{
forward = TRUE;
DEBUG(trap = 1;)
}
else
{
GLOWWORM_delBit(gw, msgBits[--b]);
forward = FALSE;
DEBUG(trap = 2;)
}
continue;
}
// Message found
if ( (forward) && (b >=
BBC_MESSAGE_LENGTH_BITS + BBC_CHECKSUM_BITS) )
{
printMessage(msgBits);
messages++;
GLOWWORM_delBit(gw, msgBits[--b]);
forward =
FALSE;
DEBUG(trap =
7;)
continue;
}
// Backtracking from a 0 bit
if ( (!forward) && (0 ==
msgBits[b]) )
{
if (b <
BBC_MESSAGE_LENGTH_BITS)
{
msgBits[b++] = 1;
if (packet[chipFromHash(GLOWWORM_addBit(gw,
1))])
{
forward = TRUE;
DEBUG(trap = 3;)
}
else
{
GLOWWORM_delBit(gw,
msgBits[--b]);
forward = FALSE;
DEBUG(trap = 4;)
}
}
else
{
GLOWWORM_delBit(gw, msgBits[--b]);
forward = FALSE;
DEBUG(trap = 5;)
}
continue;
}
// Backtracking from a 1 bit
if ( (!forward) && (1 ==
msgBits[b]) )
{
GLOWWORM_delBit(gw, msgBits[--b]);
forward =
FALSE;
DEBUG(trap =
6;)
continue;
}
printf("Decoder failed to catch
condition.\n");
}
GLOWWORM_del(gw);
return messages;
}
int countMarks(uint8_t *packet)
{
int marks = 0;
for (int i = 0; i < BBC_PACKET_LENGTH_BITS;
i++)
marks += !!packet[i];
return marks;
}
void printPacket(uint8_t *packet)
{
if (BBC_PACKET_LENGTH_BITS <= 64)
{
printf(" ");
for (int i = 0; i <
BBC_PACKET_LENGTH_BITS; i++)
printf("%c",
(i%10)? ' ' : '0' + (i/10));
printf("\n");
printf(" ");
for (int i = 0; i <
BBC_PACKET_LENGTH_BITS; i++)
printf("%c", '0'
+ i%10);
printf("\n");
}
printf("PKT: ");
for (int i = 0; i < BBC_PACKET_LENGTH_BITS;
i++)
{
printf("%c", packet[i]? 'X' :
'.');
if ((i > 0) && !(i %
64))
printf("\n
");
}
printf("\n");
}
double percentage(double numerator, double denominator)
{
return 100.0 * (numerator / denominator);
}
int main(void)
{
MyBMP *bmp = MyBMP_new(WIDTH, HEIGHT);
MyBMP_setFilename(bmp, OUT_BMPFILENAME);
//GLOWWORM_test(2);
FILE *readFILE = fopen("message_tx.txt", "r");
char input[128];
uint8_t *packet = bbc_encode(NULL, (uint8_t *) "Hello
World.", bmp);
while( fgets(input, 128, readFILE))
{
bbc_encode(packet, (uint8_t *)
input, bmp);
}
DEBUG(printPacket(packet);)
int marks = countMarks(packet);
printf("Marks in packet: %i (%6.2f%%
density)\n",
marks, percentage(marks,
BBC_PACKET_LENGTH_BITS));
printf("Found %i messages in packet\n",
bbc_decode(packet));
MyBMP_save(bmp);
MyBMP_del(bmp);
return EXIT_SUCCESS;
}
In: Computer Science