Assignment1Amit amit(1)

docx

School

Algoma University *

*We aren’t endorsed by this school

Course

1206

Subject

Computer Science

Date

Feb 20, 2024

Type

docx

Pages

8

Uploaded by EarlFlyMaster1252

Report
Assignment 1 1 . . COSC1047- Computer Science II Name: Amit ID: 239614620 . . Exercise 1: Pseudocode: Class MyPoint Private Fields x as double y as double Constructor() Set x to 0 Set y to 0 Constructor(x as double, y as double)
Set this.x to x Set this.y to y Method getX() as double Return x Method getY() as double Return y Method distance(point as MyPoint) as double Calculate distance using formula sqrt((x - point.x)^2 + (y - point.y)^2) Return calculated distance Method distance(x as double, y as double) as double Calculate distance using formula sqrt((this.x - x)^2 + (this.y - y)^2) Return calculated distance Static Method distance(point1 as MyPoint, point2 as MyPoint) as double Return distance using point1.distance(point2) End Class Main Program Create point1 as MyPoint with (0, 0) Create point2 as MyPoint with (10, 30.5) Print distance between point1 and point2 using MyPoint.distance(point1, point2) End Program Source Code:
Output: Uml Diagram: Exercise 2: Pseudocode:
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Class Course Private Fields courseName as string students as array of string numberOfStudents as integer capacity as integer = 100 Constructor(courseName as string) Set this.courseName to courseName Initialize students array with capacity Set numberOfStudents to 0 Method addStudent(student as string) If numberOfStudents equals capacity Double the size of students array End If Add student to students array at index numberOfStudents Increment numberOfStudents Method getStudents() as array of string Return a copy of students array with length numberOfStudents Method getNumberOfStudents() as integer Return numberOfStudents Method getCourseName() as string Return courseName Method dropStudent(student as string) Find index of student in students array If student is found
Remove student from array Shift remaining elements left by one position Decrement numberOfStudents End If Method clear() Initialize students array with capacity Set numberOfStudents to 0 End Class Main Program Create course as Course with "Computer Science" Call course.addStudent("Student1") Call course.addStudent("Student2") Call course.addStudent("Student3") Display course.getStudents() Call course.dropStudent("Student2") Display course.getStudents() Call course.clear() Display course.getStudents() End Program Source Code: Output: Uml Diagram:
Exercise 3: Pseudocode: Import BigDecimal and MathContext classes Define class ApproximateE
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Define the main method Initialize a MathContext object 'mc' with precision 25 Declare a BigDecimal 'e' and assign it the value of 1 (for the starting value of e) Declare a BigDecimal 'factorial' and assign it the value of 1 (for calculating factorial) For i from 1 to 1000 Multiply 'factorial' by 'i' using the MathContext 'mc' for precision Add the result of 1 divided by 'factorial' to 'e', using 'mc' for precision If i is in {100, 200, ..., 1000} Print the value of 'e' along with the current value of 'i' End Define End class Source Code: Output: