CSE 110 – ASSIGNMENT # 5
Maximum points : 20 What This Assignment Is About:
• Object Oriented Programming (Chapter 8) – Encapsulation – Implementing classes – Implementing methods – Object construction – Constructors
• Methods (Chapter 5) – Definition and Invocation.
Your programming assignments require individual work and effort to be of any benefit. Every student must work independently on his or her assignments. This means that every student must ensure that neither a soft copy nor a hard copy of their work gets into the hands of another student. Sharing your assignments with others in any way is NOT permitted.
Violations of the University Academic Integrity policy will not be ignored. The university academic integrity policy is found at http://www.asu.edu/studentlife/judicial/integrity.html
Use the following Coding Guidelines:
• Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc).
• Keep identifiers to a reasonably short length. • User upper case for constants. Use title case (first letter is upper case) for classes. Use lower
case with uppercase word separators for all other identifiers (variables, methods, objects). • Use tabs or spaces to indent code within blocks (code surrounded by braces). This includes
classes, methods, and code associated with ifs, switches and loops. Be consistent with the number of spaces or tabs that you use to indent.
• Use white space to make your program more readable. • Use comments after the ending brace of classes, methods, and blocks to identify to which block it
At the beginning of each programming assignment you must have a comment block with the following information: /*————————————————————————- // AUTHOR: your name // FILENAME: title of the source file // SPECIFICATION: description of the program // YOUR Lab Letter and Name of the TA for your Closed lab // FOR: CSE 110- homework #- days and time of your class // TIME SPENT: how long it took you to complete the assignment //———————————————————————-*/ Part 1: There is no part 1 for this assignment
Part 2: Programming (20 points):
Write a class definition (not a program, there is no main method) named Geek (saved in a file Geek.java) that models a person who is a geek. For our purposes, a geek is someone who delights in answering all sorts of questions, such as “what is the sum of all numbers between two numbers?”, “If a number is even?, and etc. A Geek has a name and also keeps track of how many questions s/he has answered.
Your Geek class must have:
• only two instance variables – the Geek’s name and number of questions asked so far
Methods Description of the methods
public Geek (String name) Sets the Geek’s name and the number of questions asked
public String getName() takes no parameters and returns the Geek’s name as a String (don’t count this request in the total questions)
public int getNumberOfQuestions() It takes no parameters and returns as an integer how many questions s/he has been asked (don’t count this request in the total)
public boolean isEven (int num1, int num2)
It takes two integers and returns a boolean value indicating if the sum of the numbers is even or not
public boolean allTheSame(int x, int y, int z)
Determines if the three arguments are all equal. returns true if all the same, otherwise false
public int sum (int num1, int num2)
It takes two integers and computes and returns an integer which is the sum of all numbers between the two inclusive (include the numbers in the sum) – for full credit this method should work even if the two numbers are the same (the sum is just one of the numbers) or if the first number is larger than the second. Also, you cannot assume which number will be greater.
public String repeat(String str, int n)
Repeats a given input string. return string repeated n times
public int digits(int n) Determines number of digits in a number. It returns the number of digits
public String middle(String str)
Finds the middle substring of a string, and return a string containing the middle substring. Determine if string length is odd or even if it is even print two characters if it is odd print one character
Save the Geek class in a file called Geek.java and Assignment5.java in the same folder. Assignment5.java is a test driver code for the Geek class.
Assignment5.java will ask a user to enter one of the following commands. Based on the user’s choice, the program performs corresponding operation. This will be done by calling (invoking) one of the methods you defined in the
Geek class. The program will terminate when the user enters ‘q’. Command Options ———————————– a: Get name b: Num of questions asked c: Is it Even d: All the same e: Sum between two integers f: Repeat g: Number of Digits h: Middle of String ?: Display q: Quit Here is the description for each option: a: asks for the Geek’s name b: returns the number of questions asked so far c: asks for two integers and returns Boolean indicating if the sum is even or odd d: asks for three positive integers and returns true if they are the same, false otherwise e: asks for two integers and returns the sum between two integers f: asks for a string and repeats count number of times g. asks for an integer and counts how many digits it has h: returns the middle character of a string if is odd, 2 characters if it is even ?: displays the menu q: quits
Helpful hints for doing this assignment:
· work on it in steps – write one method, test it with a test driver and make sure it works before going on to the next method
· always make sure your code compiles before you add another method · your methods should be able to be called in any order
Sample Output (user input is in RED):
Command Options ———————————– a: Get name b: Num of questions asked c: Is it Even d: All the same e: Sum between two integers f: Repeat g: Number of Digits h: Middle of String ?: Display q: Quit
Please enter a command or type ? a Eisenstein Please enter a command or type ? b 0 Please enter a command or type ? c Enter two numbers: 4 5 Sum is not even Please enter a command or type ? f Enter a string: hi How many times to repeat: 4 hihihihi Please enter a command or type ? g Enter a number: 5 The number 5 has 1 digits Please enter a command or type ? g Enter a number: 77854 The number 77854 has 5 digits Please enter a command or type ? h Enter a string: java The middle of string “java” is av Please enter a command or type ? h Enter a string: hello The middle of string “hello” is l Please enter a command or type ? b 6 Please enter a command or type ? c Enter two numbers: 4 5 Sum is not even
Please enter a command or type ? d Enter 3 integers: 4 5 6 All are NOT the same Please enter a command or type ? d Enter 3 integers: 4 4 4 All the same! Please enter a command or type ? e Enter the first number: 5 Enter the second number: 10 The sum between 5 and 10 is 45 Please enter a command or type ? e Enter the first number: 10 Enter the second number: 5 The sum between 10 and 5 is 45 Please enter a command or type ? q Press any key to continue . . .
Submit your homework by following the instructions below:
• Go to the course web site (my.asu.edu), and then click on the on-line Submission tab. • Submit your Assignment5.java and Geek.java files on-line. Make sure to choose HW5
from drop-down box.
Important Note: You may resubmit as many times as you like until the deadline, but we will only mark your last submission.
NO LATE ASSIGNMENTS WILL BE ACCEPTED
- Maximum points : 20 What This Assignment Is About:
- Use the following Coding Guidelines: