0% found this document useful (0 votes)
5 views

Assignment 2

Uploaded by

fukchuntse
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Assignment 2

Uploaded by

fukchuntse
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CSCI1580 Visual Programming

Assignment 2
Deadline: 25-Oct-2022 (Tue) 23:59

Objectives
This assignment allows you to practice using:

 Console Applications
 Methods
 Loop Statements

Task
A prime number is a positive integer that is greater than one and has no divisor other than one and itself.

In this assignment, your task is to use Visual Basic to develop a Console Application to read two integers,
namely r1 and r2, from the user and list all the prime numbers in the given range. For example, if r1 = 1
and r2 = 13, the program will output 2, 3, 5, 7, 11, and 13. Note that even if r1 = 13 and r2 = 1, the
program will print the same output. Note that it is possible to have no prime numbers within the range.

You cannot assume that the user input is always valid. If the user input is not valid (e.g., not an integer),
the program will print an appropriate message and ask the user to input again. This process will repeat
until the input is valid (see the Sample Run section for more details).

Hardcoding all possible prime numbers in the program is not allowed. In this assignment, you are
required to define and invoke the following method:

Private Function isPrime(ByVal n as Integer) As Boolean

to decide whether n (the parameter) is prime. The function should not print anything, but it will return
True if n is prime and return False if n is not prime. A naive way to test whether n, which is larger than 2,
is prime is to loop through all the numbers between 2 and n – 1 (can this be a smaller number?) and
check whether n is divisible by these numbers. If n is not divisible by all these numbers, it is prime;
otherwise, it is not prime. There are rooms for improvement for this naive method, and you are
welcome to implement a more efficient way for testing whether n is prime.

1
Sample Run
Underlined bold characters in blue are inputs from the user.

Sample Run 1 Sample Run 2


Please enter r1: Please enter r1:
1 13
Please enter r2: Please enter r2:
13 1
List of prime number(s) between 1 and 13: List of prime number(s) between 1 and 13:
2 2
3 3
5 5
7 7
11 11
13 13

Sample Run 3 Sample Run 4


Please enter r1: Please enter r1:
-5 five
Please enter r2: ERROR: the input is not an integer.
-5 Please enter r1:
List of prime number(s) between -5 and -5: 5.0
No prime numbers within the range. ERROR: the input is not an integer.
Please enter r1:
5
Please enter r2:
eleven
ERROR: the input is not an integer.
Please enter r2:
11.1
ERROR: the input is not an integer.
Please enter r2:
11
List of prime number(s) between 5 and 11:
5
7
11

Assessment Scheme
 Correctness 90%
 Program readability (E.g., comments, meaningful variable names) 10%

Testing Platform
Microsoft Visual Studio Community 2022, Windows 10

2
Saving the Project Files
Please use “Save All” to save all the project files in one location but not “Save <file_name>.vb As” to
save some files in other locations. Otherwise, we may not be able to open your submission successfully.

Declaration
As required by the University, please place the following declaration as the comment in the beginning of
your VB source code.

' CSCI1580 Visual Programming


'
' Assignment <assignment_number>
'
' I declare that the assignment here submitted is original except for source
' material explicitly acknowledged. I also acknowledge that I am aware of
' University policy and regulations on honesty in academic work, and of the
' disciplinary guidelines and procedures applicable to breaches of such policy
' and regulations, as contained in the following University website:
' http://www.cuhk.edu.hk/policy/academichonesty/.
'
' Student Name: <your_name>
' Student ID: <your_student_ID>
' Date: <date>

Submission
Please follow the following steps to submit your work.

Step 1: Zip your Visual Basic project folder into a zip file Assg2_<your_student_id>.zip, where
<your_student_id> should be replaced by your student ID (e.g., Assg2_1234567890.zip).

The default project folder for Visual Studio 2022:

C:\Users\<user_name>\source\repos\<project_name>

Step 2: Go to BlackBoard and login your account by using your OnePass (CWEM) password.

Step 3: Go to the page for CSCI1580, choose Assignments => Assignment 2

Step 4: Upload your zip file prepared in Step 1.

Resubmissions are allowed. But only the latest one will be counted. 10% of your marks will be
deducted for late submission each day after the deadline. Late submissions more than three days will
not be graded.

You might also like