Results 1 to 17 of 17
  1. #1
    camaro01 is offline Member
    Join Date
    Jan 2011
    Posts
    52
    Rep Power
    0

    Default 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Can you post more information about your problem and more code? Please check out the "smart questions" link in my signature links below that can give you good suggestions on what information can help us help you. The link has helped me with my questions and can help you too.

    Much luck and welcome!

  3. #3
    camaro01 is offline Member
    Join Date
    Jan 2011
    Posts
    52
    Rep Power
    0

    Default

    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:

    Java 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.
    Last edited by Fubarable; 02-20-2011 at 04:33 PM. Reason: code tags added

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  5. #5
    camaro01 is offline Member
    Join Date
    Jan 2011
    Posts
    52
    Rep Power
    0

    Default

    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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by camaro01 View Post
    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.

  8. #8
    camaro01 is offline Member
    Join Date
    Jan 2011
    Posts
    52
    Rep Power
    0

    Default

    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?

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by camaro01 View Post
    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?

    Java 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

    Java 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.

    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.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by camaro01 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    camaro01 is offline Member
    Join Date
    Jan 2011
    Posts
    52
    Rep Power
    0

    Default

    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:

    Java 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.
    Last edited by camaro01; 02-20-2011 at 05:29 PM.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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 [code] above your pasted code and the tag [/code] below your pasted code like so:

    Java Code:
    [code]
      // your code goes here
      // notice how the top and bottom tags are different
    [/code]
    You can edit your post above and don't have to post a new message to show the tags.

  13. #13
    camaro01 is offline Member
    Join Date
    Jan 2011
    Posts
    52
    Rep Power
    0

    Default

    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:
    Java Code:
    	public String getMovieCode() 
          	{ 
    	   m = movieCode; // I was thinking about creating this m variable and using it
                                           // in the switch statement
            	   return movieCode; 
          	}
    Last edited by camaro01; 02-20-2011 at 05:31 PM.

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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:

    Java Code:
       public void computeCost() {
          double basicMovie = 3.95;
          
          switch (movieCode.charAt(0)) {
          case 'A':
             // code for when code is 'A'
             break;

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Also this isn't doing what you expect it to:

    Java 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.

  16. #16
    camaro01 is offline Member
    Join Date
    Jan 2011
    Posts
    52
    Rep Power
    0

    Default

    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"?

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.

Similar Threads

  1. School project help!
    By Addez in forum Advanced Java
    Replies: 5
    Last Post: 09-17-2010, 10:37 PM
  2. Need help with school project!
    By jaxxx in forum New To Java
    Replies: 5
    Last Post: 03-25-2010, 08:55 PM
  3. School project: network program via java any ideas?
    By Sernomicus in forum New To Java
    Replies: 2
    Last Post: 12-07-2009, 03:46 PM
  4. so very lost on school project....
    By Moltisanti in forum New To Java
    Replies: 19
    Last Post: 09-23-2009, 08:39 AM
  5. school project help
    By justin8790 in forum New To Java
    Replies: 5
    Last Post: 03-22-2009, 09:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •