Starting from:

$30

HW5 Shell Programming

CS507 Computing Foundation for Computational Science HW5
Shell Programming

Submission command: submit minlong CS507 HW5
Instruction to electronic submission: http://cs.boisestate.edu/∼cs221/SubmissionProcedure.html
• The written assignment can be done in a pure text format (*.txt, for example, problem1.txt) on
Onyx.
• The programming assignment should be presented with source codes.
• Each problem should have its own working directory, such as HW2/prob1, HW2/prob2 ... For
example, the following table shows the structure of HW1 from the user “student1” and how to
submit HW to us through Onyx
1 [ student1@onyx :HW1] $ l s −l
2 drwxr−x−−−. 2 s t u d e n t 1 S tuden t s 13 Sep 19 2021 prob1
3 drwxr−xr−x . 3 s t u d e n t 1 S tuden t s 14 Sep 19 2021 prob2
4 [ student1@onyx :HW1] $ cd prob1 /
5 [ minlong@onyx : prob1 ] $ l s
6 −rw−r−−−−−. 1 s t u d e n t 1 S tuden t s 943 Sep 19 2021 problem1 . t x t
7 $ cd . .
8 [ student1@onyx :HW1] $ pwd
9 /home/ s t u d e n t 1 /CS507/HW1
10 [ student1@onyx :HW1] $ submit minlong CS507 HW1
Listing 1: A sample structure of homework and submission procedure.
• Your source codes (if any) must compile and run on Onyx.
• Documentation is important and proper comments are expected in your source code.
– comments giving description of: purpose, parameters, and return value if applicable
– other comments where clarification of source code is needed
– proper and consistent indentation
– proper structure and modularity
Don’t ask us or your classmates directly for solutions (it happened); just try as much as possible.
Be patient and enjoy coding!
1
Programming Problems
The grading will be based on the functionality, quality, simplicity, and user-friendliness of the code.
The simpler, better-designed, shorter code will get a higher score compared to barely runnable code.
1. (10 pts) Class and Object. Please help a busy professor to make a code using a class to
compute students’ letter grades based on the average of his/her mid-term and final exams. The
mapping between the score and the grade can be found below.
Average Score ≥ 90 ≥ 80 ≥ 70 ≥ 60 < 60
Letter Grade A B C D F
The first program is called lgStudent.py. It should include at test main() and a LGstudent
class. In the class, we expect a constructor and the following functions setName(), setMidterm(),
setFinal(), calcGrade() and str () functions.
The second program is the client code called grade.py, which should import the LGstudent class
from lgStudent.py.
The program will read input from the terminal and provide output like:
1 $ python g r ade . py
2 Enter s tuden t ’ s name : Amy
3 Enter s tuden t ’ s g r ade on midterm exam : 90
4 Enter s tuden t ’ s g r ade on f i n a l exam : 92
5 Do you want t o c o n ti n u e (Y/N) ? Y
6 Enter s tuden t ’ s name : Fred
7 Enter s tuden t ’ s g r ade on midterm exam : 84
8 Enter s tuden t ’ s g r ade on f i n a l exam : 97
9 Do you want t o c o n ti n u e (Y/N) ? N
10
11 NAME GRADE
12 Amy A
13 Fred A
Listing 2: Sample output.
2. (10 pts) Class and Object. Create a class named Fraction, having instance variables for numerator and denominator. It also should have the following method: constructor, setNumerator(),
getNumerator(), setDenominator(), getDenominator(), str (), a method GCD() that computes
the greatest common divisor reduces a fraction to lowest terms by dividing the numerator and
denominator by their greatest common divisor. Save the class in the file fraction.py.
(a) (5pts) Reduce a Fraction. Write a program that import the Fraction class and requests a
fraction as input and reduces the fraction to lowest terms.
1 $ python f r a c t i o n −reduce . py
2 Enter numerator o f f r a c t i o n : 12
3 Enter denominator o f f r a c t i o n : 30
4 Reduction t o l o w e s t terms : 2/5
Listing 3: Sample output.
(b) (5pts) Convert a Decimal to a Fraction Write a program that converts a decimal number to
an equivalent fraction.
2
1 $ python f r a c t i o n −c o n v e r t . py
2 Enter a p o s i t i v e decim al number l e s s than 1 : . 7 5
3 Converted t o f r a c t i o n : 3/4
Listing 4: Sample output.
3. (10pts) ) Class and Object. Proceed to Checkout Write a program that checks out the items
in the user’s cart on a shopping website. The program should use a class named Purchase to hold
the information about a single item purchased (that is, description, price, and quantity) and a
class named Cart to hold a list whose items are objects of type Purchase.
The Purchase class should include constructor, setDescription(), getDescription(), setPrice(),
getPrice(), setQuantity(), getQuantity() functions.
The Cart class should include constructor, addItemToCart(), getItems() and calculateTotal()
functions.
1 $ python purchase−c l i e n t . py
2 Enter d e s c r i p t i o n o f a r t i c l e : apple
3 Enter p r i c e o f a r t i c l e : 2
4 Enter q u a n ti t y o f a r t i c l e : 3
5 Do you want t o e n t e r more a r t i c l e s (Y/N) ? Y
6 Enter d e s c r i p t i o n o f a r t i c l e : banana
7 Enter p r i c e o f a r t i c l e : 1
8 Enter q u a n ti t y o f a r t i c l e : 10
9 Do you want t o e n t e r more a r t i c l e s (Y/N) ? N
10
11 ARTICLE PRICE QUANTITY
12 appl e $2 . 0 0 3
13 banana $1 . 0 0 10
14
15 TOTAL COST: $16 . 0 0
Listing 5: Sample output.
4. (10pts) Inheritance. Let’s extend the code obtained from the Problem 1.
Create two subclasses LGstudent(Student) and PFstudent(Student) that inherit all the properties
and methods of a superclass Student. LGstudent class handles students letter grades. PFstudent
class handles whether students Pass or Fail a class.
Obviously, the PFstudent class will have a calcGrade() method different from that in the LGstudent class as shown below.
Average Score ≥ 60 < 60
Pass or Fail Pass Faill
Next write a drive code student-client which imports student and accomplish the following jobs.
1 $ python s tuden t−c l i e n t . py
2 Enter s tuden t ’ s name : Bob
3 Enter s tuden t ’ s g r ade on midterm exam : 70
4 Enter s tuden t ’ s g r ade on f i n a l exam : 85
5 Enter c a t e g o r y (LG o r PF) : LG
6 Do you want t o c o n ti n u e (Y/N) ? Y
7 Enter s tuden t ’ s name : Amy
3
8 Enter s tuden t ’ s g r ade on midterm exam : 92
9 Enter s tuden t ’ s g r ade on f i n a l exam : 83
10 Enter c a t e g o r y (LG o r PF) : LG
11 Do you want t o c o n ti n u e (Y/N) ? Y
12 Enter s tuden t ’ s name : Fred
13 Enter s tuden t ’ s g r ade on midterm exam : 75
14 Enter s tuden t ’ s g r ade on f i n a l exam : 76
15 Enter c a t e g o r y (LG o r PF) : PF
16 Do you want t o c o n ti n u e (Y/N) ? N
17
18 NAME GRADE
19 Amy B
20 Bob C
21 Fred Pass
22 Number o f l e t t e r −g r ade s t u d e n t s : 2
23 Number o f pass−f a i l s t u d e n t s : 1
Listing 6: Sample output.
5. (10pts) Inheritance. Rock, Paper, Scissors Write a program to play a three-game match of
“rock, paper, scissors” between a person and a computer. See Fig. 7.15. The program should use
a class named Contestant having two subclasses named Human and Computer. After the person
makes his or her choice, the computer should make its choice at random. The Contestant class
should have instance variables for name and score. (Note: Rock beats scissors, scissors beats
paper, and paper beats rock.)
1 $ python game . py
2 Enter name o f human : Amy
3 Enter name o f computer : DeepMind
4
5 Amy, e n t e r your c h oi c e : r oc k
6 DeepMind c h o o s e s s c i s s o r s
7 Amy: 1 DeepMind : 0
8
9 Amy, e n t e r your c h oi c e : s c i s s o r s
10 DeepMind c h o o s e s paper
11 Amy: 2 DeepMind : 0
12
13 Amy, e n t e r your c h oi c e : paper
14 DeepMind c h o o s e s paper
15 Amy: 2 DeepMind : 0
16
17 AMY WINS
Listing 7: Sample output.
4

More products