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.
Related
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;
}
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);
}
}
I am trying to input a date in the format dd mm yyyy and have to return in the format e. g. Tuesday, 29th September. I'm new to Java and am not sure if my values from the day, month, year classes are being returned to the main. There is an error for the Date date1 = new Date(day1, month1, year1) saying it is undefined and to make it a constructor. If I make it a constructor and run the program I get values back like lab2.Day#659e0bfd. Not sure where to go from here.
package lab2;
public class Calendar {
//assume all months have 30 days and that 1 January is a Monday;
public static void main(String[] args) { // e.g. input: 29 09 2015
int in1 = Integer.parseInt(args[0]);
int in2 = Integer.parseInt(args[1]);
int in3 = Integer.parseInt(args[2]);
Day day1 = new Day(in1, in2); // string
Month month1 = new Month(in2); // string
Year year1 = new Year(in3); // integer
Date date1 = new Date(day1, month1, year1); //
date1.printName(); // e.g. "The date is Tuesday, 29th of September, 2015
}
}
package lab2;
public class Day {
private int day;
private int month;
private int code;
private String name; //e.g. Tuesday
public Day(int dy, int mth ){
day = dy;
month = mth;
code = ((day*month) % 7);
// Assigning the day variable number to text //
switch (code) {
case 0: name = "Monday";
break;
case 1: name = "Tuesday";
break;
case 2: name = "Wednesday";
break;
case 3: name = "Thursday";
break;
case 4: name = "Friday";
break;
case 5: name = "Saturday";
break;
case 6: name = "Sunday";
break;
default: System.out.println("Incorrect Day Entered");}
}
}
package lab2;
public class Month {
private int month1;
private String monthName;
public Month(int temp1){
month1=temp1;
switch (month1) {
case 1: monthName = "January";
break;
case 2: monthName = "February";
break;
case 3: monthName = "March";
break;
case 4: monthName = "April";
break;
case 5: monthName = "May";
break;
case 6: monthName = "June";
break;
case 7: monthName = "July";
break;
case 8: monthName = "August";
break;
case 9: monthName = "September";
break;
case 10: monthName = "October";
break;
case 11: monthName = "November";
break;
case 12: monthName = "December";
break;
default: System.out.println("Incorrect Month Error");}
}
}
package lab2;
public class Year {
private int yearNumber;
public Year(int temp2)
{
yearNumber=temp2;
}
}
package lab2;
public class Date {
private String day;
private String month;
private int year;
public Date(String temp1, String temp2, int temp3){
day = temp1;
month = temp2;
year = temp3;
}
public void printName() {
System.out.println("The date is " +day+ ", " +month+ ", " +year);
}
}
There is an error for the "Date date1 = new Date(day1, month1, year1)"
saying it is undefined and to make it a constructor.
Since there is no such parameterised constructor in Date class like Date(Day d, Month m, Year y). Its having constructor Date(String temp1, String temp2, int temp3). So parameter types are different.
Instead of
Date date1 = new Date(day1, month1, year1);
use
Date date1 = new Date(day1.name, month1.monthName, year1.yearNumber);
There is an error for the "Date date1 = new Date(day1, month1,
year1)" saying it is undefined and to make it a constructor. If I make
it a constructor and run the program I get values back like
"lab2.Day#659e0bfd". Not sure where to go from here.
You are getting lab2.Day#659e0bfd because you haven't implemented toString() method. Implement toString() method in your Day, Month and Year class and you'll get the desired result. Check the code below-
public class Day {
private int day;
private int month;
private int code;
private String name; //e.g. Tuesday
public Day(int dy, int mth ){
day = dy;
month = mth;
code = ((day*month) % 7);
// Assigning the day variable number to text //
switch (code) {
case 0: name = "Monday";
break;
case 1: name = "Tuesday";
break;
case 2: name = "Wednesday";
break;
case 3: name = "Thursday";
break;
case 4: name = "Friday";
break;
case 5: name = "Saturday";
break;
case 6: name = "Sunday";
break;
default: System.out.println("Incorrect Day Entered");}
}
#Override
public String toString() {
return name;
}
}
Similary, you can put the toString() method in Month class
public class Month {
private int month1;
private String monthName;
public Month(int temp1){
month1=temp1;
switch (month1) {
case 1: monthName = "January";
break;
case 2: monthName = "February";
break;
case 3: monthName = "March";
break;
case 4: monthName = "April";
break;
case 5: monthName = "May";
break;
case 6: monthName = "June";
break;
case 7: monthName = "July";
break;
case 8: monthName = "August";
break;
case 9: monthName = "September";
break;
case 10: monthName = "October";
break;
case 11: monthName = "November";
break;
case 12: monthName = "December";
break;
default: System.out.println("Incorrect Month Error");
#Override
public String toString() {
return monthName;
}
}
Your Year class -
public class Year {
private int yearNumber;
public Year(int temp2)
{
yearNumber=temp2;
}
#Override
public String toString() {
return yearNumber;
}
}
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
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.