How are primitive and reference types different an examples of how copying works differently for these two types.
In: Computer Science
Write a program that calculates how many digits an integer value acquired via scanf() contains. A correctly functioning program should produce the following output, where the user entered the value 374:
Enter a number: 374
The number 374 has 3 digits
You may assume that the input integer has no more than four digits.
Use if statements to test the number. For example,
if the integer value entered by the user is between 0 and 9, it has
one digit. If the integer value is between 10 and 99, it has two
digits.
Rewrite the program without using if or switch statements. Instead use a do-while loop.
When implemented correctly, the user should be able to input any integer value (that will fit in a storage variable of type int) and its number of digits will be correctly determined without you needing to implement special logic for each possible quantity digits.
Hint:
The number of digits is very simply related to the number of
times the input integer value may be divided by
10.
In: Computer Science
Question: Not sure how to proceed with question below:
In this task, task 2(a) will be extended so that an array of
five Person objects are created. Write the comma
separated values format of each object in separate lines in a file
named persons.txt. Make use of the
toString() method of Person to obtain comma separated values.
Create a hyperlink pointing to the file persons.txt and include it
on the task page. When the user clicks
on the link, the browser can then download it.
This is question 2a below: not need to be answered
Task 2: Chapter 14: page name=task2.php 40 marks
This task consists of two different subtasks.
(a) [15 marks]
Code a class named Person, which has three data properties to store
the name, a telephone number and
an e‐mail address of a person.
The class must have a constructor to initialise all three data
properties, three set methods and three get
methods to change and access the data properties of the
class.
The class must also have a toString() method with an argument named
$csv initialised to a default
boolean value true. By default, this function must return the value
of data properties in comma separated
values (refer to Chapter 23, Section named ‘How to read and write
CSV data’). On the other hand, if the
method is called with boolean value false, it returns a string
representation of the data properties in a
different format (for example, see showAll() method in Section ‘How
to loop through an object’s
properties’ in Chapter 14).
Create an object of the class Person, invoke all six set and get
methods, and invoke toString() with
both boolean values. Display the results returned by toString()for
both method calls.
In: Computer Science
Write functions that do the following in Python:
i) A function that takes 2 arguments and adds them. The result returned is the sum of the parameters.
ii) A function that takes 2 arguments and returns the difference,
iii) A function that calls both functions in i) and ii) and prints the product of the values returned by both.
In: Computer Science
Examples:
The Lab is divided into a set of example and exercise activities related with this topic and students are required to perform all activities and show (and discuss) the output of activities to the instructor.
Example 1: Implement the Stack Push Operation with Overflow condition
Declare an array of size 5 and implement the Push Operation with overflow condition; you can type the following code as a hint:
#include<iostream>
using namespace std ;
#define MAX 5
struct Stack {
int S[MAX];
int top;
};
Stack st;
st.top = -1;
int item;
if(st.top == (MAX-1))
cout<<"Stack Overflow\n" ;
else
{
cout<< "Enter the item to be pushed in stack : " ;
cin>> item ;
st.top++;
st.S[st.top] = item;
}
Complete this implementation as a full program to PUSH as many elements as the user wants (or till the Stack is full).
Example 2: Implement the Stack Pop Operation with Underflow condition
Declare an array of size 5 and implement the Pop operation; you can type the following code as a hint:
int item;
if (st.top == -1) /* stack empty condition */
cout<< "Stack Underflow\n" ;
else
{
item = st.S[st.top];
st.top-- ;
cout<< "Element popped from stack is :" << item ;
}
Complete this implementation as a full program to POP as many elements as the user wants (or till the Stack is empty).
Example 3: Display the contents of Stack
Display the contents of Stack; you can type the following code as a hint:
int i;
if(st.top == -1)
cout<<"Stack is empty\n" ;
else
{
cout<< "Stack elements :\n" ;
for(i = st.top; i >=0; i--)
cout<< st.S[i] ; }
Exercise
Combine all the three examples above to implement one complete program of Stack
In: Computer Science
QUESTION 5
Consider the following Die class:
class Die {
public:
void setValue(int x);// assign x to num
int getValue(); // return num
void roll(); // set num with a random number ranging 1-6
Die(); // constructor
private:
int num;
};
Which of the following C++ code segment will correctly find that how many times a Die object will be rolled until the value '6' is obtained.
Die myDie; |
||
Die myDie; |
||
Die myDie; |
||
Die myDie; |
3 points
QUESTION 6
Consider the following statements.
struct supplierType {
string name;
int supplierID;
};
struct paintType {
supplierType supplier;
string color;
string paintID;
};
paintType paint;
Which stataement is correct?
paint.name="Sherwin-Williams"; |
||
paintType.supplyType.name="Sherwin-Williams"; |
||
paint.supplier="Sherwin-Williams"; |
||
paint.supplier.name="Sherwin-Williams"; |
In: Computer Science
In C programming
Declare two 2d arrays n0[MAX_ROW][MAX_COL], n1
[MAX_ROW][MAX_COL].
Then, loop through the 2d array and save the results in n0 and
n1:
for (i=0; i<MAX_ROW; i++)
for (j=0; j<MAX_COL;j++){
//calculate number of
zeros around entry a[i][j]
n0[i][j] =
//calculate number of
ones around entry a[i][j]
n1[i][j] =
}
Set the MAX_ROW and MAX_COL to a small number and display all three 2d arrays on the screen to verify that the calculations are correct. You may still use command-line inputs as in the example program to set the actual size of the array (must be less than or equal to MAX_ROW, MAX_COL.
In: Computer Science
(a) Write code segments to perform the following:
(i) declare and create a boolean array flagArray of size 5
(ii) declare and initialize an array amount which contains 39.8, 50 and 45
(iii) declare a Keyboard array of size 3 with name keyboard and initialize it with Keyboard objects using one statement
(b) A incomplete definition of a class Cash is given below: public class Cash { private double value[] = {10.5, 20, 5.5, 7.8}; }
(i) Copy and put it in a new class. Write a method toString() of the class, which does not have any parameters and returns a string containing all the values separated by newlines. When the string is printed, each value should appear on a line in the ascending order of their indexes. Copy the content of the method as the answers to this part.
(ii) Write another class TestCash in a separate file with a method main() to test the class Cash. In main(), create a Cash object cash and print its values by calling toString(). Run the program. Copy the content of the file and the output showing the message as the answers to this part.
(iii) Add a method decreasePercent(int index, double percent) to the Cash class which decreases the element with subscript index of the value array by the percentage percent without returning anything. Also add a method getValue(int index) to return value[index]. Copy the content of the methods as the answers to this part.
(iv) Add another method countBelow(double threshold) to the Cash class which returns the number of values which are less than threshold. Copy the content of the method as the answers to this part.
(v) Add another method minimumIndex() to the Cash class which returns the index of the minimum value in the array. Copy the content of the method as the answers to this part. You can assume there is only one minimum value.
(vi) Add another method trimmedMean() to the Cash class which returns the average value which excludes the (unique) largest value and (unique) smallest value in the calculation. You should use minimumIndex() to get the index of the minimum value. Note that this method should work without any modifications when the number of values, which is at least three, is changed. Copy the content of the method as the answers to this part.
(vii) Perform the tasks listed below in main() of TestCash: * print the second value from the left, decrease it by 10% using the method decreasePercent(), and print the new value; * calling countBelow()to count the number of values less than 15 and then print it; * print the index of the minimum value and the trimmed mean. Run the program. Copy the content of the class and the output as the answers to this part.
In: Computer Science
You are to create a class called Person. You should create the definition of the class Person in a file person.h and the functions for Person in person.cpp. You will also create a main program to be housed in personmain.cpp.
A Person will have attributes of
A Person will have the following methods
personmain.cpp should perform the following actions
In: Computer Science
In: Computer Science
For this exercise, you are going to write your code in the FormFill class instead of the main method. The code is the same as if you were writing in the main method, but now you will be helping to write the class. It has a few instance variables that stores personal information that you often need to fill in various forms, such as online shopping forms.
Read the method comments for more information.
As you implement these methods, notice that you have to store the result of concatenating multiple Strings or Strings and other primitive types. Concatenation produces a new String object and does not change any of the Strings being concatenated.
Pay close attention to where spaces should go in theString, too.
FormFillTester has already been filled out with some test code.
Feel free to change the parameters to print your own information.
If you don’t live in an apartment, just pass an empty String for
the apartment number in setAddress.
Don’t put your real credit card information in your program!
When you run the program as written, it should output
Dog, Karel 123 Cherry Lane Apt 4B Card Number: 123456789 Expires: 10/2025
public class FormFill
{
private String fName;
private String lName;
private int streetNumber;
private String streetName;
private String aptNumber;
// Constructor that sets the first and last name
// streetNumber defaults to 0
// the others default to an empty String
public FormFill(String firstName, String lastName)
{
}
// Sets streetNumber, streetName, and aptNumber to the given
// values
public void setAddress(int number, String street, String apt)
{
}
// Returns a string with the name formatted like
// a doctor would write the name on a file
//
// Return string should be formatted
// with the last name, then a comma and space, then the first
name.
// For example: LastName, FirstName
public String fullName()
{
}
// Returns the formatted address
// Formatted like this
//
// StreetNumber StreetName
// Apt AptNumber
//
// You will need to use the escape character \n
// To create a new line in the String
public String streetAddress()
{
}
// Returns a string with the credit card information
// Formatted like this:
//
// Card Number: Card#
// Expires: expMonth/expYear
//
// Take information as parameters so we don't store sensitive
information!
// You will need to use the escape character \n
public String creditCardInfo(int creditCardNumber, int expMonth,
int expYear)
{
}
}
public class FormFillTester
{
public static void main(String[] args)
{
FormFill filler = new FormFill("Karel", "Dog");
filler.setAddress(123, "Cherry Lane", "4B");
System.out.println(filler.fullName());
System.out.println(filler.streetAddress());
System.out.println(filler.creditCardInfo(123456789, 10,
2025));
}
}
In: Computer Science
Radix sort was proposed for sorting numbers, but if we
consider a number as a string of digits, the
algorithm can be considered as a string sorting algorithm. In this
project you are asked to
implement a radix sort algorithm forsorting strings in ascending
order. The input to your algorithm
should be a (multi)set S = [S1, S2, . . . , Sn] of strings each of
which is of length m over the English
alphabet [A…Z, a…z]. The output should be the set sorted in
ascending lexicographical order.
Notes:
• For simplicity you my limit the number of stings n (or |S| ) to
100 and the number of characters
(digits) m in each string Si to 50.
• You should use the cursor implementation of the linked list data
structure to store characters in
your buckets.
• You are allowed to use only character comparisons. Thus, you are
not allowed to use the String
library functions.
• You should sort all characters starting at position k, where k is
the position of the most
significant digit dk in the string ( e.g., in the string “BirZeit”
t is the character in the least
significant digit while B is the character in the most significant
digit).
Example: the set {data, structures, and, algorithms, in, C} should
be sorted as follows:
a l g o r i t h m
a n d
C
d a t a
i n
s t r u c t u R e s
Notice that in the first pass, the strings and and algorithm would
be placed in one bucket since the
first character in both is a. In the second pass, however, the
strings will be reordered since l comes
before n in lexicographical order. Same applies for the remaining
passes
In: Computer Science
Create and initialize a string array, write and use 3 functions.Write a program.
Create a string array in the main(),called firstNameArray, initialize with7 first namesJim, Tuyet, Ann, Roberto, Crystal, Valla, Mathilda
Write a first function, named searchArray, that passes two arguments: a single person’s name and the array reference, into a function,This function should then search the array to see if the name is in the firstNameArray. If found, it will return the array index where the name is found, else it return the number 7.(This function needs 3 parameters –see code examples above)
Write the code in the main program to call/use the searchArray function. Check the return value for the index value returned. Print the name using the index number(0 to 6), or prints ‘name not found’ if it return a 7.
Write a second function, print AllNames, that will print all the names in the array.Pass the array into the function. (This function needs two parameters –see code example above)
Write the code in the main program to call/use this printAllNames function.
Write a third function, called deleteName, that will delete a name from the array. Check first to see if the name is in the array, before you try to delete it(use the searchArray function).If you find the name, then write “ “ to the location in the array. ... Just making spot blank.(This function requires 3 parameters –see code examples above).
Call the printAllNames function to print the array to verify you have deleted a value.Print out the array... if the spot/index in the array is blank do not print it.
C++
In: Computer Science
PROGRAM DEVELOPMENT: Analyze and write the code for the
following requirement:
The sales department wants to store the sales data of their
employees. Assume that there are 20 employeesand they all work in
three areas. Declare appropriately and accept the sales data from
the user.
In: Computer Science
Write a java program (use value returning method) that gives simple math quizzes. The program should display two random integers from 1 to 100 that are to be added, such as:
47
+ 29
The program allows the user to enter the answer. If
the answer is correct, display “congratulations”. If the answer is
incorrect, the program should show the correct answer.
Your result should look like, for example:
47
+ 29
Enter your answer: 1
Wrong, the right answer is 76
In: Computer Science