联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> Java编程Java编程

日期:2018-10-04 09:47

Programming in Java

Fundamentals of Programming in Java

Exercises 1 (Formative)

University Information System Tools

Marks available: 100*

Date issued: 01/10/2018

Deadline: 1700, 22/10/2018

Feedback: 1700, 26/10/2018

*This set of exercises constitutes a formative assignment for all students. That means you must submit

your work but it will not contribute to your final module mark. You will get feedback on your work.

Submission instructions

Submit your work by the deadline above. The accepted submission formats are a zip archive or a jar

archive. Submit only .java source files in your archive. Do NOT submit .class files. Please do not submit

Eclipse (or other IDE) projects.

Do not output anything to the console unless explicitly asked to.

Where applicable, your class for each of the exercises must have the name shown in the exercise heading.

In each of these exercises, you are not restricted to only creating the required methods. If you want to,

you can create others. However, you must create at least the required methods.

Do not use any of the Java collections for these exercises, e.g. ArrayList.

You must not use regular expressions in your solution. Use of regular expressions in your solution will

result in a mark of zero.

1

Exercise 1a: GradeChecker [15 marks]

Write a program that asks the user to enter a student grade and then outputs either “Pass.” for a pass

grade or “Fail.” for a fail grade. The pass mark is 40 out of 100. A grade of less than 40 is a fail and

a grade of 40 or more is a pass. If the input grade is not valid (according to the definition below), the

program should output the message “Invalid input” and end.

Your program should have a class called GradeChecker with a main method.

Your GradeChecker class must have the following methods:

public static boolean isValid(int grade)

This method returns true if the input grade is in the range [0,100] (0 to 100 inclusive), and false if not.

This method must do nothing else apart from this. For example, this method must not get the user input

or do any other processing.

public static boolean isPass(int grade)

This method returns true if the input grade is 40 or above, and false if not. This method must do

nothing else apart from this. This method does not validate the input grade.

For this exercise you can use the class that has been provided. You just need to complete the two methods

listed above.

Exercise 1b: MeanGrade [20 marks]

Do not use any of the Java collections for this exercise, e.g. ArrayList.

Create a program that will compute the mean grade of four input grades. Each grade represents the mark

for one module, thus four module marks are to be entered. The user should be prompted to enter four

grades. The user should press the enter key after each grade. The program will then compute and output

the mean of the grades entered. The program should not allow more than four grades to be entered. The

user may, however, enter fewer than four grades. If fewer than four grades are entered, the missing grades

should be assumed to be zero.

If an input grade is not valid (according to the definition in Exercise 1a), the program should ignore that

grade when computing the mean.

When designing your program, consider how you would extend it to handle a larger number of grades, if

requested. For example, how would your program handle 20 grades? 100? Your program only needs to

handle four grades but do think about this design question.

Your program should have a class called MeanGrade with a main method.

Your MeanGrade class must have the following method:

public static double computeMean (int[] grades)

2

This method computes the mean of the grades in the array grades. Note that it returns a double. This

method should not assume that the array has length four1.

Remember that, in Java, you can find the length of an array as follows. For the array grades, the expression

grades.length evaluates to the size (length) of the array. This value will always be the size that the array

was created with and not a count of how many values have been set in the array.

Exercise 1c: ResultChecker [20 marks]

Create a program that outputs a result for a student based upon the mean grade of eight module grades

and a project mark.

The rules for the marks are the same as for the above exercises. The pass mark is 40 and a valid grade is

in the range [0,100]. These rules apply to individual module grades and to the project.

The result is computed as follows:

If any individual module grade or the project grade is not valid then the result is “ERROR”.

If any single grade (for a module or the project) is less than 40, the result is “FAIL”.

If the mean mark for the modules is less than 40, the result is “FAIL”.

If the criteria for “ERROR” and “FAIL” do not apply, and either the mean grade or the project grade is

less than 50, the result is “PASS”.

If the criteria for “ERROR”, “FAIL” and “PASS” do not apply, and either the mean grade or the project

grade is less than 70, the result is “MERIT”.

If the mean module grade and the project grade are greater than or equal to 70, the result is “DISTINCTION”.

Your program should have a class called ResultChecker with a main method.

Your ResultChecker class must have the following method:

public static String getResult(int[] grades)

This method returns one of the String values above based upon the input grades and the project grade.

Thus, the return value of this method will be one of {“ERROR”, “FAIL”, “PASS”, “MERIT”, “DISTINCTION”}.

Note that there is no separate parameter for the project grade so you must include it in the array grades.

Exercise 1d: IDCreator [15 marks]

Create a program that receives a student’s name as input and then outputs a three-character ID for that

student. This three-character ID would be used to create a longer, unique ID but that is not a part of this

1The reason for this is that you might wish to use this method in a different application in which the number of values is

not restricted to four.

3

particular program.

Prompt the user to enter a student’s name. You can assume that the student’s name will have either two

or three parts to it, i.e. it will either be first-name last-name or first-name middle-name last-name2

.

The ID is created as follows. If the user enters a three part name, the ID number is created by taking

their initials, i.e. the first character of each part of the name, converted to lower-case characters where

necessary. For example, the name “David Robert Jones” would lead to an ID of drj.

If the user enters a two-part name, the ID is again created by taking their initials, converted to lower-case

characters where necessary, but this time substituting an “x” character for the missing middle name. For

example, the name “Richard Starkey” would lead to an ID of rxs.

All upper-case letters must be converted to lower-case in the ID.

Your class must contain the following method:

public static String createID(String input)

This method receives the input name as a single String and returns the generated ID as a String.

If the input string is not valid then this method should return the value null. The input is not valid if:

 It is null.

 It is empty.

 It does not contain either two or three parts.

Exercise 1e: PasswordChecker [15 marks]

Do not use regular expressions in your solution for this exercise.

Write a program that checks to see if an input password is valid or not. In this exercise, a valid password

has the following characteristics:

 It contains at least eight characters.

 It contains at most 12 characters.

 It contains only alphabetic characters, digits and the underscore (‘ ’) character.

 It does not start with a digit.

 It has a mix of upper-case and lower-case characters.

Your class must contain the following method:

public static String checkPassword(String input)

2

I appreciate that this is inadequate for some names but the restriction is needed to minimise the complexity of the task.

4

This method receives a possible password as input and returns only the String“OK” if the password is

valid. If the password is not valid then you can return the reason(s) for that as the return value. This is

not a part of the requirements, however, and you don’t need to do that. You can simply return null for

an invalid password.

Exercise 1f: ShortAddressFinder [15 marks]

Do not use regular expressions in your solution for this exercise.

Write a program that can create a ‘short address’ from a longer one. The program should be able to receive

an address that has a fixed format but is of arbitrary length, and convert that to a specified shorter format.

All input addresses will have the following format:

address-line-1, address-line-2, ..., address-line-n, postcode

Note that there is a space character after each comma.

All postcodes will have the following format:

adddaa.

Where a is an alphabetic character and d is a digit3

.

For example, the address 56 Clapham Gardens, Stoke Newington, London, N145PX has the given format.

As does 3 Acacia Ave, E178PU.

The short address that your program generates must have the following format:

address-line-1 postcode

Note that there is no comma in this string but there is a space between address-line-1 and postcode.

For the examples above, the short addresses would be:

56 Clapham Gardens N145PX and 3 Acacia Ave E178PU.

Your class must contain the following method:

public static String findShortAddress(String input)

This method receives a long address as input and returns the generated short address. If the input is null

or if it is empty, this method must return null.

If the input is incorrectly formatted, the method should return null. You must check that the postcode

has the correct format for postcodes, as defined above.

3This is not a realistic format since it excludes many real postcodes


版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp