In this lab you will practice with functions (methods) and conditionals. Feel free to work in pairs.
Ideally, you should've tried to understand the lecture material on functions and conditionals before you try to solve these problems. In reality though these problems will help you understand the lecture material better. So, in practice, you may go back and forth between these problems and the lecture material as you try to complete your understanding of the lecture material. By the time you are done with these lab exercises though, hopefully, you will have understood the lecture material enough to be able to solve the problems in the assignment. By the time you finish the assignment, you will deepen all the knowledge you are expected to learn in the lecture material. Keep that in mind as you study this semester.
One more thing: By now you should feel quite comfortable using vim or emacs or another simple text editor, the javac command, and the java command. If you are not, then you are already behind! I can guarantee you that things will get quite complicated and challenging very quickly as soon as we start adding loops, and arrays with functions and conditionals!
Create a class named Practice
in a file named Practice.java
.
In this class, do as instructed below.
1. Define a function named add2
which takes 2 integers and returns the sum of the two integers. For
example, the line:
int sum = add2(34, 56);should assign
90
to sum
.
Verify the result of your function call by printing the value
of sum
using System.out.println
in the caller,
perhaps main
.
2. Define a function named greatestNumber
which takes 2 doubles and returns the greatest of the two numbers. For
example, the line:
double greatest = greatestNumber(54.6, 106.4);should assign
106.4
to greatest
.
Verify the result of your function call by printing the value
of greatest
using System.out.println
.
3. Define a function
named addStrings
which takes three String
values
and returns the concatenated result of the three strings. For
example, the line:
String s = addStrings("Apple", "Banana", "Carrot");should assign the following string to
s
that looks something like:
"AppleBananaCarrot"Verify the result of your function call by printing the return value using
System.out.println
in the caller, again
main
. Using the same main
just add more lines.
4. In your main
print a message asking the user to "Enter a name" and then read a line of text
from the keyboard and save that text in a variable. Check if the value in the variable is the same as your
first name, printing out "It matches" if it is the same. Otherwise if it does not match, print out "Does not match".
Be sure to use the .equals()
method on the String class to check if the values
are the same and not the ==
operator.
Verify the result by running the program and entering in both the matching and not matching text.
Voting
Implement a class named VotingProjection
that does the following:
In your main
use a Scanner
to read in the names and a vote count (integer) for 3 candidates in an election.
Call a method generateResults
that takes the three names (Strings)
and the three vote counts for the candidates.
The method should do the following:
Candidate [name] won with [percent]% of the popular vote.