CCIT4020 Introduction to Computer

洛雨听花發表於2024-12-08

CCIT4020 Introduction to Computer Programming Assignment 3 – Section C General guidelines:

  1. Use concise and direct techniques/program codes we learn in our course.
  2. Useless or over-complicated techniques/program codes may be ignored or penalized.Students should reference our course materials.
  1. Proper brief comments are required, at least at the top of each source code file.
  2. A penalty will be applied if a student’s name and student ID are missing in any file or ithe file names are changed.
  1. A penalty will be applied for late submissions, but only if the submissions are within halfan hour.
  1. 0 mark will be awarded if the submission is later than half an hour or if plagiarism isidentified.
  1. No email submissions will be accepted.
  2. Files A3Q1.py, A3Q2.py, and A3Q3.py may be imported into other Python programs.Ensure that the main() function will not be executed incorrectly during the import

process. SECTION C: Long Questions [30 marks] Question C1 [10 marks] Write your answer in the provided file, A3Q1.py. Fill in your name and student ID in the designated section. You need to write code related to factorial calculation. Three functions are specified asfollows:

  1. Function getFactorialNumList()
  2. Accept one parameter n, an integer number expected
  3. The function body calculates the factorials of all numbers from 0 to the inputnumber n, stores all of them to a Python list, and returns the list to the caller.
  1. Function dispFactorialNumList()
  2. Accept one parameter fList, a list of factorial numbers expected
  3. The function body shows 代寫 CCIT4020 Introduction to Computerthe factorial numbers contained in the input parameterlist when there are data elements in this list. Otherwise, display the message “Nofactorial numbers”. Refer to the sample outputs for display details.
  1. Function main() accepts no input parameter and returns no value
  2. Ask the user to enter an integer n.
  3. Call the function above to obtain its factorial number.
  4. Display the result as the sample display on the console.
  5. Assume the inputs are integers only. No need to consider non-integer inputs.
  6. Call the main() function to start the programHint: Refer to the general guidelines to determine how to call the main() function.2

Completed by Chan Siu Ming, 40202020

Note: The dashed line contains 20 dashes. The user inputs are in green.3Question C2 [10 marks] Write your answer in the provided file A3Q2.py. Fill in your name and student ID in the

designated section. This application is used in a fruit store to calculate the amount spent on each order, maintainrecords, and generate a daily report or log file for all orders.The fruit names and their corresponding prices (per gram) are stored in a text file named“productsList.txt” with the format “<fruit name in str> = <price in float>”. Each line inthe file contains one ‘fruit-price’ pair, as shown in the sample below:To implement this application, you need to define a few functions:Write a function read_data() with the filename as the input parameter and return alist of strings holding all contents of this file.

  1. Write a function list_to_dict() with aList as input parameter and return adictionary. Below is test case for this function:
  1. Write a function cust_spend() with adict as parameter. Iterate through all the itemkeys in the dictionary adict, and prompt the user to enter the weight (in grams) foreach fruit. The function should calculate the total amount of the order and return it as anoutput in float format. Below is the test case for this function:>>> f_test=cust_spend(dictTest)How many grams of the fruit 'Fruit 1'[price:0.02]? 100 How many grams of the fruit 'Fruit 2'[price:0.03]? 100 How many grams of the fruit 'Fruit 3'[price:0.04]? 100 >>> print(f_test)9.0Write a function main() as the main loop of this application. Inside the main() function,do the following:
  1. Load the fruit-price information by opening the given text file"productsList.txt" and reading the contents into a variable ls_allPrices as a list.
  1. Convert this list to a dictionary dict_ProductPrice by calling thecorresponding function.
  1. Use a while True loop to ask the user for customer ID, and store it in a variablecustomerID. Print the greeting messages (see the sample output) and ask theuser to enter the weights for each fruit (by calling the cust_spend() function).>>> listTest = ['Fruit 1=0.02\n','Fruit 2=0.03\n','Fruit 3=0.04\n']>>>dictTest=list_to_dict(listTest)>>> print(dictTest){'Fruit 1': 0.02, 'Fruit 2': 0.03, 'Fruit 3': 0.04}Print the total amount of the order. Exit this loop when the user enters the word'end'.
  1. Store each order in a tuple with the format “<customerID in str>,<totalAmountin str>” Create a variable todayOrder as a list and add each tuple record tothe list todayOrder.
  1. Display the variable todayOrder after the user entered the word 'end' as acustomerID. The total amount should be displayed with 2 decimal places.
  1. The program output should adhere to the Sample Output provided below:Sample Output 1:Sample Output 2 (No order has been made.).Note:
  • All user inputs are in green.
  • Assume the user inputs for the weights are all in numeric data types.
  • Assume the provided file “productsList.txt” contains the sample data and exists

in the same directory as the Python file A3Q2.py.

  • Ensure that all mentioned variables (ls_allPrices, todayOrder,dict_ProductPrice, customerID) are used and assigned appropriate values.

Hint: Refer to the general guidelines to determine how to call the main() function. 4Question C3 [10 marks] Write your answer in the provided file A3Q3.py. Fill in your name and student ID in the

designated section.

The constructor accepts three arguments for assigning related fields and define thedata fields, properly assigning the input parameters accordingly.The method __str__() returns a string representation of the object in the formatshown in sample outputs.The method isAtriangle() accepts no argument and returns True if the threeinput sides can form a triangle; returns False otherwise.The method isRightAngleTriangle() accepts no argument and returns True ifthe triangle is a right-angle triangle; returns False otherwise.The method area() accepts no argument, calculates the area of a triangle, andreturns it.Write a function isSimilarTriangles() to compare two triangles.The function accepts two Triangle objects as input parameters and returns True if theyare similar triangles; returns False otherwise.Write a function main() which continuously asks user to input 6 values: (side1a,side2a, side3a) and (side1b, side2b, side3b) for the Triangle object T1 and Trespectively. Formulate two triangles and continue the process until T1 and T2 areeither similar triangles OR either one is a right-angle triangle.Exit the program when the triangles are similar triangles, or either one is a right-angletriangle. When both criteria are satisfied, display for similar triangles should beprioritized.If no two triangles are formed, display a message asking the user to reinitiate bothtriangles.If the triangles are neither similar triangles nor none of them is a right-angle triangle,display a message asking the user to reinitiate both triangles.You can assume that the user will only input valid numeric values.Call the main() to start to program execution.

Hint: Refer to the general guidelines to determine how to call the main() function.The program output should adhere to the Sample Program Output provided below:5Sample Output 1:Note: In the last line of the Sample Output 1, the program is still waiting for user input.Sample Output 2:Sample Output 3:

Note: The user inputs are in green.— END OF PAPER — 6

相關文章