Java Programming
Assignment 6-1
We have covered the “Methods” this week. Apply the concepts that we have learnt in class and come up with a “problem scenario” for which you can create a “solution”. Utilize any/all of the examples from the book and class that we discussed. You program should be interactive and you should give a detailed statement about what the “description of the program – purpose and how to use it”. You should also use a “good bye” message. Remember to discuss in your “reflection” the advantage of using “methods” – we discussed “re-usability” and “modularity”. Write detail comments on the program.
You need to include all the following:
Static Methods
Library methods and user written methods
Non Static Methods (Class & Object)
Methods with arguments
Methods without arguments
Conditional statements
Loops
For Example: (you could come up with any other program) - use your creativity Welcome to my “multifunction” program.
In this program I have an interactive program to show the use and benefits of “methods” in Java program. You will see a “selection menu” that would allow you to use the program. Choose 1. For your “compound interest calculator” of your “retirement nest” income, 2. For your “Interactive input of Employee attributes and then display of their attributes”, 3. For your…. Thank you for using my “multifunction” program. Have a nice day
In: Computer Science
03 Prepare : Checkpoint B
Objective
Demonstrate basic class methods (member functions) in
Python.
After completing (or while completing) the
preparation material for this week, complete the following
exercise.
Introduction
Recall from your mathematics classes that a complex
number is composed of two parts, a real part and an imaginary part.
We could write a complex number in the form "3 + 4i" where 3 is the
real part and 4 is the imaginary part. For this program, we will
create a new class for complex numbers, and write methods (member
functions) to prompt for them and display them.
Instructions
Write a Python 3 program according to the
following:
Create a class for a Complex number that has two
member variables, "real" and "imaginary".
Create an initializer function for this class that
sets each part to 0.
Create a prompt method (member function) that will
prompt the user for the two values and set them
appropriately.
Create a display method (member function) that
displays the complex number in the form "3 + 4i". For this
assignment, you need not worry about handling negative numbers, or
about simplifying the display if either value is 0.
Then, in your main function you should create two new
complex numbers. Display them (which should show 0 + 0i), prompt
the user for each one, and display them again.
To help you see how to work with these values, a main
function is provided for you. Your task is to create the Complex
class to make this work.
The file to start with is found at:
/home/cs241/check03b.py. You can copy it to your directory with the
following command:
cp /home/cs241/check03b.py .
Don't forget the "." at the end, it tells linux to
copy it to your current directory.
Sample Output
The following is an example of output for this
program:
The values are: 0 + 0i 0 + 0i Please enter the real part: 3 Please
enter the imaginary part: 4 Please enter the real part: 6 Please
enter the imaginary part: 10 The values are: 3 + 4i 6 + 10i
Automatic Grading Script (TestBed)
This assignment is pass/fail. In order to receive
credit, it must pass the auto-grading test script (TestBed). You
can run this script as many times as you like while you are working
on the assignment. The same test script will be run by the
instructor for credit, so you should not be surprised at the
result.
In addition, because the point of this exercise is to
help you practice the use of classes. You must use classes
to receive credit for this assignment.
To run the test script, log into the CS Department
Linux system and run the testBed command, providing the class and
test script to run as well as the Python program to use, for
example:
testBed cs241/check03b check03b.py
In: Computer Science
I have listed below two functions. Please add these two functions to StudentMath class.
Test it from the Test class
public int divideByZero(int gradeA, int gradeB, int gradeC)
{
int numberOfTests = 0;
int gradeAverage = 0;
try
{
gradeAverage = (gradeA + gradeB + gradeC)/numberOfTests;
}
catch(ArithmeticException ex)
{
System.out.println("Divide by zero exception has occured" +
ex.getMessage());
}
return gradeAverage;
}
public int ArrayOutOfBoundEception() {
int i = 0;
try {
// array of size 4.
int[] arr = new int[4];
// this statement causes an exception
i = arr[4];
// the following statement will never execute
System.out.println("Hi, I want to execute");
}catch(ArrayIndexOutOfBoundsException ar) {
System.out.println("Array index out of bound exception: " +
ar.getMessage());
}catch(Exception ex) {
System.out.println("An exception occured" + ex.getMessage());
}finally {
System.out.println("Finally block is always executed. It is used to
close the resources such as database connection or open file,
etc.");
}
return i;
}
Part 2
Write the function StudentFeePaid() in the Student class. . (Assume the fee is paid. you can hard code it)
It will return a string which will have the value "PAID" which means the string which you will return will be like String fee = "PAID".
You will write the try/catch and finally block in this function. In the finally block add the following
System.out.println("Finally block was executed");
Overide this function in the StudentMath class [Hint Use Override attribute and super.StudentFeePaid()]
The returned string will have "PAID" in it. You will modify it to "PAID Thank You" in the overridden function
Test it in the StudentTest Class.
---------------------------------------------------------------------------------------------------------
public class Student
{
int gradeA;
int gradeB;
int gradeC;
int average;
Student(int gradeA, int gradeB, int gradeC)
{
this.gradeA = gradeA;
this.gradeB = gradeB;
this.gradeC = gradeC;
}
public int Average()
{
this.average = (this.gradeA + this.gradeC + this.gradeC)/3;
return average;
}
public String AssignLetterGrade()
{
String grade = "";
if(average >= 90)
{
grade = "A";
}
else if((average < 90) && (average >= 80))
{
grade = "B";
}
return grade;
}
public static void main(String[] args)
{
int grade1 = 80;
int grade2 = 90;
int grade3 = 60;
System.out.println("Instantiate Student class");
Student stu = new Student(grade1, grade2,
grade3);
int Average = stu.Average();
System.out.println("The average grade is: " +
Average);
String strStudentGrade =
stu.AssignLetterGrade();
System.out.println("The letter grade is: " +
strStudentGrade);
}
}
------------------------------------------------------------------------------------------------------
public class StudentMath extends Student
{
public StudentMath(int gradeA, int gradeB, int
gradeC)
{
// invoking base-class
constructor
super(gradeA, gradeB,
gradeC);
}
@Override
public int Average()
{
int newAverage = super.Average() +
5;
return newAverage;
}
@Override
public String AssignLetterGrade()
{
String grade =
super.AssignLetterGrade();
if (grade.equals("A"))
{
grade = grade +
" : Excellent";
}
return grade;
}
}
-------------------------------------------------------------------------------
public class Test
{
public static void main(String[] args)
{
// TODO Auto-generated method
stub
int gradeA = 90;
int gradeB = 91;
int gradeC = 92;
StudentMath stMath = new
StudentMath(gradeA, gradeB, gradeC);
int average =
stMath.Average();
System.out.println("The average is
" + average);
String grade =
stMath.AssignLetterGrade();
System.out.println(grade + "
Excellent");
}
}
In: Computer Science
please, solve this problem.:
implementing with java language
create a bank account management system according to the following specifications:
BankAccount Abstract Class contains the following constructors and methods:
BankAccount(name, balance): a constructor that creates a new account with a name and starting balance.
getBalance(): a method that returns the balance of a specific account.
abstract deposit(amount): abstract method to be implemented in both Checking and SavingAccount classes.
abstract withdraw(amount): abstract method to be implemented in both Checking and SavingAccount classes.
messageTo Client (message): used to print a specific message to a client.
toString(): a method for printing accounts information.
CheckingAccount Class: A class that inherits from the Abstract Class BankAccount and implements both deposit () and withdraw ().
Deposit process in checking account has some constrains. Client can deposit any amount using checks and limit of $10,000.00 of cash deposit.
Client can withdraw any amount he wants.
SavingAccount Class: A class that inherits from the Abstract Class BankAccount and implements both deposit () and withdraw ().
A client can deposit any amount using checks and a limit of $5,000.00 of cash deposit. A 5% interest should be calculated on the total.
Client can only withdraw 20% of the total balance every 3 months.
Use interfaces for both checkingAccount and savingAccount classes.
BankAccountDriver class: this class is to test your code. The test should be done by creating an array of type BankAccount that stores different bank accounts types for different clients and perform some processing by calling the implemented methods on these accounts.
In: Computer Science
Fundamentals of Programming
USING JAVA
Please copy here your source codes, including your input and output screenshots. Please upload this document along with your source files (i.e., the .java files) on Blackboard by the due date.
1.
(Display three messages) Write a program that displays Welcome to Java, Welcome to Computer Science, and Programming is fun.
2.
(Convert feet into meters) Write a program that reads a number in feet, converts it to meters, and displays the result. One foot is 0.305 meter. Here is a sample run:
Enter a value for feet: 16.5
16.5 feet is 5.0325 meters
3.
(Financial application: compute taxes) Listing 3.5, ComputeTax.java, gives the source code to compute taxes for single filers. Complete this program to compute taxes for all filing statuses.
4.
(Financial application: payroll) Write a program that reads the following information and prints a payroll statement:
Employee’s name (e.g., Smith)
Number of hours worked in a week (e.g., 10)
Hourly pay rate (e.g., 9.75)
Federal tax withholding rate (e.g., 20%)
State tax withholding rate (e.g., 9%)
A sample run is as follows:
Enter employee’s name: Smith
Enter number of hours worked in a week: 10
Enter hourly pay rate: 9.75
Enter federal tax withholding rate: 0.20
Enter state tax withholding rate: 0.09
Employee Name: Smith
Hours worked: 10.0
Pay Rate: $9.75
Gross Pay: $97.5
Deductions:
Federal Withholding (20.0%): $19.5
State Withholding (9.0%): $8.77
Total Deduction: $28.27
Net Pay: $69.22
5.
(Find the largest n such that n3<12,000) Use a while loop to find the largest integer n such that n3 is less than 12,000.
In: Computer Science
a.LinkedList < T > and ArrayList
< T > are interfaces
b.List < T > is a concrete class
c.LinkedList < T > and ArrayList< T > are concrete
classes
d.List < T > is an interface
enqueue 'a'
enqueue 'b'
enqueue 'c'
dequeue
dequeue
What remains in the queue after these operations complete?
a. 'a'
b. 'a', 'b'
c. 'b', 'c'
d. 'c'
push 'a'
push 'b'
pop
peek
push 'c'
pop
What value(s) is/are on the stack after these operations are complete?
a. 'a', 'b', 'c'
b. 'a', 'b'
c. 'c'
d. 'a'
a. O(1), O(log n), O(n), O(n ^
2)
b. O(n log n), O(n), O(1), O(log n)
c. O(log n), O(n), O(n log n), O(n ^ 2)
d. O(l), O(n), O(n ^ 2), O(n ^ 3)
a. binary search of a sorted array is
O(log n)
b. linear search of an array is O(n)
c. traversing a linked list is O(n)
d. adding an element to the end of an arraylist that has available
space is O(n)
In: Computer Science
1. Enter user name. 2. Enter test scores. 3. Display average. 4. Display summary. 5. Quit. Selection:
c++ please
for while do while
In: Computer Science
C coding
• Implement, using structures and functions as appropriate, a
program which requires you to enter a number of points in 3
dimensions. The points will have a name (one alphanumeric
character) and three coordinates x, y, and z. Find and implement a
suitable way to stop the input loop. The program, through an
appropriate distance function, should identify the two points which
are the furthest apart. Another function should calculate the
centre of gravity of the point cloud (i.e., the average of each
coordinate for all points entered)
• The output should show a list of the points entered, then name and list the two that are furthest apart, and finally list the centre of gravity. Note: you should use pointers when passing structs to functions to get full marks.
In: Computer Science
Your textbook mentions that relational operators can be used to compare letters and words, thanks to ASCII. This simple program will ask the user to enter two letters and then two words to see if we can really "sort" these entries alphabetically with relational operators.
Prompts:
This is a Letter and Word sorting program
Enter two different letters separated by a space: [assume user
inputs: b a]
Enter two different words separated by a space: [assume user inputs: bob alex]
Output:
a goes before b
alex goes before bob
Notes and Hints:
1) Do NOT convert the user's entry to uppercase. Some of the test cases will be comparing lowercase to uppercase letters (the results are interesting!)
2) Don't worry about the possibility of the user entering the same letters/words or entering something else. Keep this program simple.
// VARIABLES
// INITIAL INPUT
cout << "This is a Letter and Word sorting program" <<
endl;
cout << "Enter two different letters separated by a space:
";
cout <<endl; // For Mimir
cout << "Enter two different words separated by a space:
";
cout << endl; // For Mimir
//Processing// LETTER ANALYSIS
// The < means letter1 is alphabetically before letter2
cout << letter1 << " goes before " << letter2
<<endl;
cout << letter2 << " goes before " << letter1
<< endl;
// WORD ANALYSIS
// The < means word1 is alphabetically before word2
cout << word1 << " goes before " << word2
<< endl;
cout << word2 << " goes before " << word1
<< endl;
In: Computer Science
Copy and paste the below code EXACTLY as shown into your Java environment/editor. Your task is to fill in the code marked as "...your code here...". A detailed explanation follows the code.
import java.util.*;
public class OddManOut {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("How many random Integers to produce?");
int num = sc.nextInt();
ArrayList<Integer> randomInts = createRandomList(num);
System.out.println("The random list is: ");
System.out.println(randomInts);
removeOdds( randomInts );
System.out.println("The random list with only even numbers:
");
System.out.println(randomInts);
}
public static ArrayList<Integer> createRandomList(int
num)
{
... YOUR CODE HERE ...
}
public static void removeOdds(ArrayList<Integer> list)
{
... YOUR CODE HERE ...
}
}
The program first asks you how many random numbers to produce (variable num). The program then calls createRandomList() which returns an ArrayList of Integers that contain random numbers between 1 and 20. The number of random numbers to produce depends on the variable num (i.e: if the user enters 50 then this method returns an ArrayList of Integers that contain 50 random numbers between 1 and 20). Once this list is displayed, the program calls removeOdds() which removes all numbers from the list that are odd leaving only the even numbers. Finally the program displays the modified list showing only even numbers.
Example output shown below:
How many random Integers to produce?
31
The random list is:
[6, 19, 8, 12, 13, 4, 12, 7, 16, 7, 21, 3, 16, 4, 17, 2, 4, 14, 2, 1, 3, 5, 7, 18, 17, 13, 10, 20, 8, 18, 20]
The random list with only even numbers:
[6, 8, 12, 4, 12, 16, 16, 4, 2, 4, 14, 2, 18, 10, 20, 8, 18, 20]
DELIVERABLES:
Upload your program below for marking. Note, for full marks you MUST NOT modify the starting code (code in red) in any way. Your task is only to fill in the "...your code here..." part of the two methods.
In: Computer Science
Write an IPO diagram and Python program that has two functions, main and determine_grade.
main – Should accept input of five numeric grades from the user USING A LOOP. It should then calculate the average numeric grade. The numeric average should be passed to the determine_grade function.
determine_grade – should display the letter grade to the user based on the numeric average:
Greater than 90: A
80-89: B
70-79: C
60-69: D
Below 60: F
Modularity: Your program should contain 2 functions: a main function to accept input from the user and calculate average and a second function to display the letter grade.
Input Validation: The test scores entered by the user should be in the range 1-100
Output: Display both the numeric average (rounded to two decimals) and the letter grade
Sample Dialog:
Enter score #1 in range 1-100: 900
Error: Re-enter score #1 in range 1-100: 90
Enter score #2 in range 1-100: 65
Enter score #3 in range 1-100: 98
Enter score #4 in range 1-100: 33
Enter score #5 in range 1-100: 75
Your average is: 72.20 which is a(n) C
In: Computer Science
Write a C program that creates and prints out a linked list of strings.
• Define your link structure so that every node can store a string of up to 255 characters.
• Implement the function insert_dictionary_order that receives a word (of type char*) and inserts is into the right position.
• Implement the print_list function that prints the list.
• In the main function, prompt the user to enter strings (strings are separated by white-spaces, such as space character, tab, or newline).
• Read and insert every string into the link list until you reach a single dot “.” or EOF.
• Print the list. Use scanf to read strings. Use strcpy() for copying strings.
Here is a sample text input:
This is a sample text. The file is terminated by a single dot: .
In: Computer Science
Question 57
A Customer must exist before an Order Header for that Customer can
be added.
True
False
Question 58
Each Supplier must supply at least one Part.
True
False
Question 59
Every Order Header must have at least one Order Line.
True
False
Question 60
Every Order Line must contain one and only one Product.
True
False
In: Computer Science
Write a MIPS Assembly Language program which runs interactively to convert between decimal, binary, and hexadecimal.
1. Request input data type.
2. Request input data.
3. Request output data type.
4. Output the data. Use any algorithm
In: Computer Science
Describe at least three algorithms (python only) to determine whether an arbitrary array A[1 .. n] contains more than n/4 copies of any value. Analyze the running time complexity of all the three methods.
In: Computer Science