# Need help with school project

• 02-20-2011, 03:23 PM
camaro01
Need help with school project
Hi everyone, I am working on a school project and I am having a lot of difficulties with the switch statement.

I have created part of the switch statement but I can't figure out of to call the variable.

Here is my switch statement block:

switch (m) // m represent rating
{
case (A): cost = basicMovie * RATING_A;
break;
case (B): cost = basicMovie * RATING_B;
break;
case (C): cost = basicMovie * RATING_C;
break;
case (D): cost = basicMovie * RATING_D;
break;
case (F): cost = basicMovie * RATING_F;
break;
}

Now, I am suppose to get the rating from the following method:

public String getMovieRating()
{
return movieRating;
}

how can I convert that movieRating into the m variable I need in my switch statement?

Thanks
• 02-20-2011, 03:36 PM
Fubarable

Much luck and welcome!
• 02-20-2011, 05:32 PM
camaro01
Thanks for the quick response and the welcome.

The assignment is to create a class that would calculate the cost of a movie based on its rating using the switch statement.

Here is my complete code:

Code:

```public class ViewerTest1 {                                     public ViewerTest1(String customer, String movieCode, double cost)                 {         customer ();         movieCode ();                   cost ();         computeCost ();       }                 public String getCustomer()               {         return customer;               }                 public String getMovieCode()               {         m = movieCode; // I was thinking about creating this m variable and using it                                     // in the switch statement                 return movieCode;               }  // I need a single letter or string for the switch block                 public Double getCost ()               {         return cost;               }                 public void computeCost ()               {         double basicMovie = 3.95;               }                           //                          switch (m) //                { //                case (A): cost = basicMovie * RATING_A; //          break; //          case (B): cost = basicMovie * RATING_B; //          break; //          case (C): cost = basicMovie * RATING_C; //          break; //          case (D): cost = basicMovie * RATING_D; //          break; //          case (F): cost = basicMovie * RATING_F; //          break;                                                                        //                    }                                  if (movieCode == "1");                                                       {                   if(movieCode [1] = ("N"))                     basicMovie += 2.50;                                                                   else if(movieCode [1]=("R"))                     basicMovie += 1.00;                                   else                                 cost=9999.99;                                     }                                                                                          public static final double RATING_A = 1.50;                 public static final double RATING_B = 1.36;                 public static final double RATING_C = 1.26;                 public static final double RATING_D = 1.06;                 public static final double RATING_F = 1.00;       private String customer;       private String movieCode;       private double cost;                 }```

I will make another post so I don't make one long post.
• 02-20-2011, 05:34 PM
Fubarable
Moderator Action: code tags added to above post.
• 02-20-2011, 05:35 PM
camaro01
I went to Oracle tutorials and it didn't help me. It only talks about int in the switch statements.

I have been "//" parts of the code so I can work one block at it time. So far I
was able to compile the code without the switch statement and the "if else if" block.

I read my books and I have searche several websites without any success. I might be looking at the problem from the wrong perspective.

Any hints would be appreciated.
• 02-20-2011, 05:37 PM
Fubarable
Sorry to be blunt but your code above is chock-full of compilation errors suggesting that you're adding code to bad code, something that shouldn't be done. You may want to start over but compile your code frequently, usually after you've added any new line or method, and not add any more code until any errors encountered are fixed. Otherwise you'll have a debugging nightmare on your hands.
• 02-20-2011, 05:38 PM
Fubarable
Quote:

Originally Posted by camaro01
I went to Oracle tutorials and it didn't help me. It only talks about int in the switch statements.

I have been "//" parts of the code so I can work one block at it time. So far I
was able to compile the code without the switch statement and the "if else if" block.

I read my books and I have searche several websites without any success. I might be looking at the problem from the wrong perspective.

Any hints would be appreciated.

First suggestion: Don't worry about switch until your compilation errors are corrected.
• 02-20-2011, 05:45 PM
camaro01
Hey no offense taken. Please be blunt, this is my first class in java programming and i am lost most of the time.

I was able to compile the program without the switch and the if else statements but you are saying it is all bad code.

I will start over and post some of my modifications.

Do you see anything that I can keep from this code? or should I just start over completetly?
• 02-20-2011, 05:50 PM
Fubarable
Quote:

Originally Posted by camaro01
Hey no offense taken. Please be blunt, this is my first class in java programming and i am lost most of the time.

I was able to compile the program without the switch and the if else statements but you are saying it is all bad code.

Was it the same code as you've posted? Does this this bit compile?

Code:

```        public ViewerTest1(String customer, String movieCode, double cost)         {         customer ();         movieCode ();           cost ();         computeCost ();       }```
Because it shouldn't since your class doesn't have the methods customer, movieCode, and cost, nor should it. Instead you probably want to initialize your class fields such as

Code:

`this.customer = customer;`

And many more lines don't compile for me. Again, I have to ask if the code you compiled is the same as the code posted.

Quote:

Do you see anything that I can keep from this code? or should I just start over completetly?
The code above should be scrapped, except the constant and field declarations look OK to me.
• 02-20-2011, 06:00 PM
JosAH
Quote:

Originally Posted by camaro01
I went to Oracle tutorials and it didn't help me. It only talks about int in the switch statements.

The tutorials should've helped you a lot because the only type allowed in a switch statement is an int; no Strings nor any other fancy stuff is allowed, just a single int expression.

kind regards,

Jos
• 02-20-2011, 06:18 PM
camaro01
Fubarable:

You are right; I post the wrong code. I have been working on this for the last few days trying to figure it out and I didn't realize I made some changes last night.

This is the code that compiles:

Code:

``` public class Viewer {           public Viewer(String customer, String movieCode, double cost)     {         customer = customer;         movieCode = movieCode;         cost = cost;         computeCost ();       }         public String getCustomer()       {       return customer;       }         public String getMovieCode()       {       return movieCode;       }         public Double getCost ()       {         return cost;       }         public void computeCost ()       {       double basicMovie = 3.95;       }                           //        switch () //      { //          case (A): cost = basicMovie * RATING_A; //          break; //          case (B): cost = basicMovie * RATING_B; //          break; //          case (C): cost = basicMovie * RATING_C; //          break; //          case (D): cost = basicMovie * RATING_D; //          break; //          case (F): cost = basicMovie * RATING_F; //          break;                                                                         //        }      //               //                { //                  if(movieCode [1] = ("N")) //                    basicMovie += 2.50;                   //                  else if(movieCode [1]=("R")) //                    basicMovie += 1.00;       //                  else //                  cost=9999.99; //            }                                                                                          public static final double RATING_A = 1.50;                 public static final double RATING_B = 1.36;                 public static final double RATING_C = 1.26;                 public static final double RATING_D = 1.06;                 public static final double RATING_F = 1.00;                 private String customer;       private String movieCode;       private double cost;                 }```
PS: How do you tag the code? I was looking fo an icon but did not see it.
• 02-20-2011, 06:21 PM
Fubarable
Quote:

PS: How do you tag the code? I was looking fo an icon but did not see it.
To tag your code highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

Code:

```[cod&#101;]   // your code goes here   // notice how the top and bottom tags are different [/cod&#101;]```
You can edit your post above and don't have to post a new message to show the tags.
• 02-20-2011, 06:22 PM
camaro01
Josh,
the tutorial was helpful. Having a problem converting the movieCode (string) into the int that goes into the Switch statement.

That was the reason I added the following:
Code:

```        public String getMovieCode()               {           m = movieCode; // I was thinking about creating this m variable and using it                                       // in the switch statement                   return movieCode;               }```
• 02-20-2011, 06:25 PM
Fubarable
As Jos mentioned, you can't switch on String, but you can on int or char, and the latter could work well for you since the String method charAt(...) can return the char:

Code:

```  public void computeCost() {       double basicMovie = 3.95;             switch (movieCode.charAt(0)) {       case 'A':         // code for when code is 'A'         break;```
• 02-20-2011, 06:27 PM
Fubarable
Also this isn't doing what you expect it to:

Code:

`customer = customer;`
You need to use this.customer on the left side else the compiler will think you're setting the parameter to itself which does nothing.
• 02-20-2011, 07:22 PM
camaro01
Thansk a lot!!!

I made the changes and I got it to compile including the switch statements. I will work on adding the if, else, statements later today.

I am currently using jgrasp to compile my programs and I just learned how to use the debugger in it. I tried running this piece of code throught it but it would not debug it because it has "No main methods, applets of MIDIlets found in file"

By any chance, are you familiar with Jgrasp? and do you know a way to fix this error without using the "main method"?
• 02-20-2011, 07:34 PM
Fubarable
This is not a JGrasp issue but a Java issue: your code must have public static void main method to run (ignoring the exceptions for now). So give it one, even if it's only temporary to be used to test your code.