Test class for switch java - java

I want to create a test class in eclipse for the following code in java how can i do that??
public class SwitchDemo {
public static void main(String[] args) {
int month = 8;
String monthString;
switch (month) {
case 1: monthString = "January";
break;
case 2: monthString = "February";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "Invalid month";
break;
}
System.out.println(monthString);
}
}

First of all, make your code testable. Best code for tests is a method that gets parameters and returns result without using a context. It's your case!
public class SwitchDemo {
public static void main(String[] args) {
assert "August".equals(monthByNumber(8));
assert "Invalid month".equals(monthByNumber(13));
}
private static String monthByNumber(int month) {
String monthString;
switch (month) {
case 1: monthString = "January";
break;
case 2: monthString = "February";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "Invalid month";
break;
}
return monthString;
}
}
Then just use assert or JUnit framework, like in answer of Jakub Hr.

Right now your code contains hardcoded value 8 and it always return August.
But it doesn't mean that you can't write a test, especially using TDD.
So let's write test first:
public class SwitchDemoTest {
#Test
public void getMonthShouldReturnAugustWhenParameterIs8() {
final String result = SwitchDemo.getMonth(8);
Assert.assertEquals("August", result);
}
}
After that your code doesn't compile so you have to add static getMonth method which accepts int parameter.
Than you can continue and write another test which forces you to get rid of hardcoded value etc.

public class SwitchDemo {
public static void main(String[] args) {
int monthNum = 8;
String monthText = "";
//Call #1
monthText = getMonthText(monthNum);
System.out.println(monthText);
//Call #2
System.out.println(getMonthText(monthNum));
}
//Extract to function for reuse
public static String getMonthText(int monthNumber){
String monthString = "";
switch (monthNumber) {
//No need to break out of the case since return automatically breaks out of function
case 1: return "January";
case 2: return "February";
case 3: return "March";
case 4: return "April";
case 5: return "May";
case 6: return "June";
case 7: return "July";
case 8: return "August";
case 9: return "September";
case 10: return "October";
case 11: return "November";
case 12: return "December";
default: return "Invalid month";
}
}
}

Remove main() method with another method like:
public static String monthOfYear(int month){
String monthString;
switch(month){
//your code until the last break; statement
}
return monthString;
}
you can access this using:
System.out.print(SwitchDemo.monthOfYear(8)); where SwitchDemo is the class that contains the
above method

Related

Problems calling my methods, can someone offer some input?

So I've been sitting here for the last 5 hours trying to understand what I'm doing wrong, I have a simple beginners task that is to create multiple different methods to find out a users name which is stored as a global variable, and which month they were born stored as an Integer. Once the Integer has been stored I use a switch statement to correspond to a certain month which I think has went fine so far. However, in calling my methods in my main class I keep getting null value returned for both the UsersName variable and also the monthString variable.
public class BirthMonthWithFunctions {
public static String UsersName;
public static void main(String[] args) {
String monthString = null;
BirthMonthWithFunctions bm = new BirthMonthWithFunctions();
BirthMonthWithFunctions.getUsersName();
bm.getUsersBirthMonthNumber();
BirthMonthWithFunctions.computeBirthMonth(0);
System.out.print(UsersName + "Was born in: " + monthString);
}
static void getUsersName() {
String UsersName = JOptionPane.showInputDialog(null, "What is your name?");
}
public int getUsersBirthMonthNumber() {
int BirthMonth = Integer.parseInt(JOptionPane.showInputDialog(null,"Which month were you born in as an integer between 1-12?"));
return BirthMonth;
}
public static String computeBirthMonth(int BirthMonth) {
int choice = BirthMonth;
String monthString;
switch(choice) {
case 1: monthString = "January";
break;
case 2: monthString = "February";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "Invalid Month";
break;
}
return monthString;
}
}
Could you please try with this piece of code that I've edited.
public static void main(String[] args) {
String monthString = null;
BirthMonthWithFunctions bm = new BirthMonthWithFunctions();
BirthMonthWithFunctions.getUsersName();
int BirthMonth = bm.getUsersBirthMonthNumber();
BirthMonthWithFunctions.computeBirthMonth(BirthMonth);
System.out.print(UsersName + "Was born in: " + monthString);
}
static void getUsersName() {
UsersName = JOptionPane.showInputDialog(null, "What is your name?");
}
Also remove the String variable declaration from the method : computeBirthMonth
It should be something like :
public static String computeBirthMonth(int BirthMonth) {
int choice = BirthMonth;
switch(choice) {..............
}
return monthString;
}

How do perform declare on switch/case?

I very new beginner in Java
Please help!
I need to declare "chk" in order to let switch know where to go.
How can I declare in something like this in order to let the switch know I need to check all 4 output and display them accordingly, not just output 1:
CheckBox chk = (CheckBox) findViewById(R.id.chk1);
switch (chk.getId()) {
case R.id.chk1:
findViewById(R.id.output1).setVisibility(visible);
break;
case R.id.chk2:
findViewById(R.id.output2).setVisibility(visible);
break;
case R.id.chk3:
findViewById(R.id.output3).setVisibility(visible);
break;
case R.id.chk4:
findViewById(R.id.output4).setVisibility(visible);
break;
}
The switch Statement
Unlike if-then and if-then-else statements, the switch statement can have a number of possible execution paths. A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types (discussed in Enum Types), the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer (discussed in Numbers and Strings).
The following code example, SwitchDemo, declares an int named month whose value represents a month. The code displays the name of the month, based on the value of month, using the switch statement.
public class SwitchDemo {
public static void main(String[] args) {
int month = 8;
String monthString;
switch (month) {
case 1: monthString = "January";
break;
case 2: monthString = "February";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "Invalid month";
break;
}
System.out.println(monthString);
}
}

Output returning Null

My class built to output the Month and month number builds and runs fine, but somewhere in my class my monthString is just being assigned null. I cannot see where I have ran into this mistake, and I apologize for my bad code. TY! ~~~
public class MonthClass
{
private int monthNum;
private String monthString;
public MonthClass(int num)
{
monthNum = num;
monthString = monthName(num);
}
public void setmonthNum(int num)
{
monthNum = num;
monthString = monthName(num);
}
public String getmonthName(int num)
{
switch (num)
{
case 1: monthString = "January";
break;
case 2: monthString = "Febuary";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString= "Augest";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "This is not a month, re-enter a number";
break;
}
return monthString;
}
public String monthName(int num)
{
return monthString;
}
public String toString()
{
return "The month number is " + monthNum + " and the month name is " + monthString;
}
}
The problem is that your code at no place modifies monthString, and its value always remains null (default value for reference types).
Instead of monthName you should be calling getmonthName (the conversion from month number to string is happening there):
monthString = getmonthName(num);
Method monthName is only a getter - it doesn't modify the monthString. Also as this method doesn't make use of any argument, you can possibly get rid of that too:
public String monthName() {
return monthString;
}
Also, in the default case in the switch statement, the assignment of non-month string is going to happen silently. You might want to print something on the console as well.

Why do i keep getting "unreachable code" and how do i fix it? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
So I'm doing this assignment for my Java class and I keep running into this error "unreachable code" and I can't find out how to fix this error or even why it is occurring. Please help!
public static void main(String[] args)
{
int suit;
int cardNumber;
Random generator = new Random();
suit = generator.nextInt (4)+1;;
String suitString;
switch (suit) {
case 1: suitString = "hearts!";
break;
case 2: suitString = "diamonds!";
break;
case 3: suitString = "clubs!";
break;
case 4: suitString = "spades!";
break;
cardNumber = generator.nextInt (13) +1;; //unreachable code
String cardNumberString;
switch (cardNumber) {
case 1: cardNumberString = "ace";
break;
case 2: cardNumberString = "2";
break;
case 3: cardNumberString = "3";
break;
case 4: cardNumberString = "4";
break;
case 5: cardNumberString = "5";
break;
case 6: cardNumberString = "6";
break;
case 7: cardNumberString = "7";
break;
case 8: cardNumberString = "8";
break;
case 9: cardNumberString = "9";
break;
case 10: cardNumberString = "10";
break;
case 11: cardNumberString = "11";
break;
case 12: cardNumberString = "12";
break;
case 13: cardNumberString = "13";
break;
You don't have a closing bracket for the switch statement after your first case 4. The next statement is after the break which means it is unreachable.
switch (suit) {
case 1: suitString = "hearts!";
break;
case 2: suitString = "diamonds!";
break;
case 3: suitString = "clubs!";
break;
case 4: suitString = "spades!";
break;
} // Need to close the switch statement
You have to close the brackets. After the case 4 the { is not closed making the code unreachable.
switch (suit) {
case 1: suitString = "hearts!";
break;
case 2: suitString = "diamonds!";
break;
case 3: suitString = "clubs!";
break;
case 4: suitString = "spades!";
break;
} //Here

Switch statement uninitialized variable

The following code gives me : The local variable str may not have been initialized
public class experiment{
public static void main(String[] args){
int day = 1;
String str;
switch (day) {
case 1 : str = "nice";
break;
}
System.out.println(str);
}
}
So, I gave str a null value, and it worked but I'm still wondering why the one on the docs work without initializing the value first, I've triple checked and I don't think I have any typos:
public class SwitchDemo {
public static void main(String[] args) {
int month = 8;
String monthString;
switch (month) {
case 1: monthString = "January";
break;
case 2: monthString = "February";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "Invalid month";
break;
}
System.out.println(monthString);
}
}
Because there's a default case statement in the switch of the documentation example. It's called if no other case statement matches. In your own code, you have no such "fallback".
Hence, there's always at least one statement that's called, which will always initialise monthString.

Categories