top of page

Introduction To Java and object-Oriented Programming


This is coursework assignment 1 (of two coursework assignments in total) for 2020–2021. The assignment asks that you demonstrate an understanding of variables, random numbers, user input with the Scanner class, the “if – else” statement, loops and static methods with their return types. In addition, this assignment considers how to write code that is readable.

Electronic file you should have:


What you should hand in: very important You are asked to submit one Java file so there is no reason to compress your submission, or to submit it in a directory. There is one mark allocated for handing in an uncompressed file – that is, students who hand in a zipped or .tar file or any other form of compressed file can only score 99/100 marks. There is one mark allocated for handing in just the file asked for without putting it in a directory; hence, students who upload their file in a directory can only achieve 99/100 marks.

There is one mark for naming the Java file that you have been asked to submit exactly as asked. This means that your Java file should be named and should contain a public class called StringManglerTwo. Please note that since Java is case sensitive is not the same file name as; please be exact. Sometimes students add identifying information to their Java files, meaning that the class name and the file name differ, and the file will not compile. For example:




  • StringManglerTwo .java (note space before full stop)

Files that do not compile because of a clash between the file name and the class name will be marked. Furthermore, submissions that do not compile for any other reason will not receive any marks.

The examiners will compile and run your Java program; students who hand in files containing their Java class that cannot be compiled (e.g. PDFs) will not be given any marks for the assignment.

The examiners wish to read your Java code, so class files will not be marked. Any student giving in only a class file will not receive any marks for the coursework assignment, so please be careful about what you upload as you could fail if you submit incorrectly.

Please put your name and student number as a comment at the top of your Java file.

Java Version

Please use Java 11 or later versions of Java for this coursework assignment.

The StringMangler class You have been given the StringMangler class. The class offers the user various options, all of which take a String as input and return the input String altered in some way.

Compile and run the program. You should see a menu, as follows:

Welcome to the String Mangler 
Available commands: VOWELCASE  swap the cases of all vowels in <string>  ASCII <string> show the ASCII character codes for every letter in <string> 
SWAP <string> swap the first and last characters of <string> RANDOM  <string> swap a random character in  with a randomly generated character 
INGIFY <string> if a word in the  ends with "ing", have it end with as many 'ing's as the word is long DEMATHS <string> remove any potential mathematical symbols from <string> 
DEDUPE <string> remove all repeated letters in <string> 
DIGRAPHS <string> capitalise the most common digraphs in English that are found in <string> 
MIDREPEAT <string> repeat the middle of words in the <string> 3 times if the word is of an even length, 5 times if it's of an odd length 
PAIRSWAP <string> swap around every 2 letters in <string> ZIP <string> split <string> in half and interleave the 2 halves together 
INSERT <string> add # or ~ to <string> after every 3 characters, depending on if the string length is even or odd REPEATSWAP <string> swap the most common repeated letters in the English language (SS <-> FF & TT <-> EE) QUIT exit the String Mangler Enter command: 

The user can choose any item from the menu. For example, the user chooses DEMATHS, followed by SWAP:

Enter command: demaths hello what is the square root of -1? Warning! Your text contained mathematics. Decontaminated text follows: hello what is the square root of ? Enter command: swap hello! !elloh

Enter command:

You should test each of the menu options and make sure you understand what they do.

Reason for using Java 11 (or later versions) The StringMangler class uses String methods extensively. One of these methods, String.join() was introduced in Java 8, and another, String.repeat() in Java 11. If you use a version of Java that is earlier than 11, the repeat() method will give a compilation error, and if the version of Java you use is also earlier than 8, the join() method will give another compilation error. You can read the documentation on these methods in the Java 11 String API:

Class variables Note that variables have a scope, generally the set of brackets inside which they are declared. When a variable is in scope it means that it can be accessed and used. Variables are generally only in scope where they are declared. For example:

for (int i = 0; i < word.length-1; i += 2) {i++;}

the variable i is only in scope inside the for loop, as it is declared in the for loop. Once the loop ends i is out of scope and cannot be accessed.

Variables can be local to methods, that is, variables declared inside a method are in scope only inside the method. Variables declared in a loop would be local to the loop. In general, if a block of code is delineated by brackets, then any variable declared immediately inside those brackets is only in scope within the brackets, and effectively does not exist outside of them.

The StringMangler class has two class variables. Class variables are static variables that are declared inside the class, but outside of all the class's methods. Class variables can be accessed by methods within the class without needing to be included in the method’s parameter list since they are in scope anywhere in the class. The StringMangler class has two class variables, a Scanner object called userInput, and a variable of Random type called random.

Writing your own methods

You are asked to alter the structure of the StringMangler class by reorganising the class into methods in order to make the class both more readable and more reflective of the objectoriented programming paradigm (in the object-oriented programming paradigm, methods should have one task to do). Try to keep your methods short, since, in general, the shorter a method is, the easier it is to read. Note that a method may have an overarching task, which the method’s name should describe, but it may be that the task undertaken by some methods could be broken down into further sub-tasks, and some or all of these sub-tasks could be performed by methods.

Suppose that the main task of a method was to take a String and return a new String with the case of all vowels in the String changed. For example, the input String “Vowels are A, E, I, O and U” would return “VOwEls ArE a, e, i, o And u”. The method’s task can be broken down into sub-tasks such as breaking the input String into characters, finding the vowels in those characters and changing their case. The developer may choose to outsource one or other of these tasks to another method. For example, the method may invoke another method that takes a char and will return the char unchanged if it is not a vowel, and in a different case if it is a vowel. Hence, the String method to change the case of all vowels in a String might be more readable if it invokes a char method called ChangeCaseOfCharIfVowel or ChangeCaseOfVowels or similar.

When making decisions about whether the methods you write should in turn invoke other methods, please always bear readability in mind (see the Appendices for details). That is, invoking sub-methods can make your methods more readable, but too much of methods invoking methods will make your class less readable. What methods to write, and if and how to break methods into sub-tasks with their own methods, is a matter for your judgement.

Changing logic or syntax To complete this coursework assignment, you will need to write some additional Java code to that already in the StringMangler class. You may wish to change some of the code given to you but remember the StringMangler class works as it is. Your challenge is not to change the behaviour of the class, but to redesign the class using methods.

Mark deduction for recursive methods When putting code into methods, you may wish to keep the logic and syntax of the statements being put into a method. You are free to rewrite existing code, provided that your revised StringMangler class does everything that the original class did. If you do rewrite existing code that is in a loop, please do not use recursion for iteration (or for any other reason). Make sure that any loops are for, while or do/while loops. If you use recursion (where methods call themselves) for iteration you will lose marks, so please make sure that none of your methods are recursive.

Note that in general, care must be taken when using recursion for iteration, as recursive methods can be very heavy on the memory, potentially leading to applications ending with a StackOverflowError. Recursion can be a good solution for certain problems, but care must always be taken to make sure that recursive methods are not using so much memory that they compromise overall performance

Coursework assignment 1

Please complete the following five Tasks:

Task 1

Note that the work of the StringMangler class is carried out entirely in the main method. Under the object-oriented programming paradigm, tasks performed in a class should be done by methods, and each method should perform only one task.

Save the StringMangler class into a new class, called StringManglerTwo. In this class rewrite the StringMangler class such that all code is within a method, except for the statements:

private static Scanner userInput; 
private static Random random; 

which are declaring class variables and should not be put into methods. Your StringManglerTwo class, when run by a user, should have exactly the same behaviour as the StringMangler class. You are not expected to write a constructor for the StringManglerTwo class, and instance methods are also not necessary. All of your new methods can be static methods

Task 2

In your StringManglerTwo class, ensure the main method only contains method invocations.

Task 3

The methods invoked by the main method of your StringManglerTwo class may vary, depending on how you have re-designed the StringMangler class, but one method all submissions should have is a method to run the user interaction loop. Your user interaction loop method should:

  • ask the user for input

  • parse the input into a command and a String

  • take appropriate action.

The method should repeat these steps until the user enters ‘quit’ and the run of the class ends. Hence your user interaction loop method should be the final method invoked by the main method. Your user interaction loop method should be doing its work with the help of other methods that it in turn, invokes.

Task 4

Make sure to follow the advice given about readability in Appendix 1, sections 1.2–1.6. In particular:

  • Give your methods, and any new variables you introduce, names that describe their purpose. Good names are partly subjective, so the examiners will accept any reasonable attempt, but clearly method names such as method1() and method2() will lose readability marks since they tell the reader nothing about the purpose and intent of the methods.

  • Format your work in a way that is both consistent and readable. The StringMangler class that you have been given is formatted in a readable way, including indenting loops and if/else statements to show the control structure and make it easy to pick out the start and end of loops and if/else blocks. Any student submitting a StringMangler class with poorly formatted code will lose some marks for this Task

Note that poorly formatted code is considered to be code that is formatted in such a way that the control structure is unclear. For example:

//Good formatting private static String getShopLocationFromUser() {  System.out.println("SHOP LOCATIONS:");  for (int i = 0; i < LOCATIONS.length; i++) {  System.out.println((i+1) + ") " + LOCATIONS[i]);  }  int choice = getIntFromUser("Your shop’s number");  return LOCATIONS[choice-1]; } 

//Poor formatting  private static String getShopLocationFromUser() { System.out.println("SHOP LOCATIONS:"); for (int i = 0; i < LOCATIONS.length; i++) { System.out.println((i+1) + ") " + LOCATIONS[i]); } int choice = getIntFromUser("The number that  corresponds to your shop"); return LOCATIONS[choice-1]; } 

Task 5

Before you start work you should run the StringMangler class and make sure that you understand what it is doing. Once you have written your new version of StringMangler, test it. Correct any mistakes you find. If you find mistakes that you cannot correct then you must document them with a brief comment at the top of your class. If your testing reveals that the class works as you expect, please write a short comment to document this (a sentence will be enough).

Make sure to test all the methods in your class. Failure to write a comment about testing will result in a loss of all marks for testing, even if your class works as it should. You should write no more than 100 words per issue found, for example if your testing discovers two issues, then you should write no more than 100 words to describe each issue, hence your total word count should be no more than 200.

The examiners will run and test your submission. Provided that any issues with the class are documented in your comment, you will receive all available marks for this Task. You will lose all marks for this Task if the examiners find issues with the class that you have not documented with your comment, or if you do not write a comment about testing.

Contact us for this Java assignment Solutions by Codersarts Specialist who can help you mentor and guide for such Java assignments.

Recent Posts

See All


bottom of page