Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Java general question

  1. #1
    Member
    Join Date
    Nov 2013
    Posts
    51
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Java general question

    I've completed task 1 of a project, it currently contains 5 classes. I have been told to create a new class that demonstrates the program working ( which it does). I was hoping someone could tell me a logical way of doing this cause i'm unsure. I'm guessing there is an easy way to just transfer the code ? Sorry for the brief thread, just don't exactly know what to put.

    write a simple class which contains a main method which demonstrates this working"


  2. #2
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Java general question

    What program? What are you trying to do?

    I'm afraid I failed out of Psychic University. I'm just not clairvoyant and can't read your mind to figure out what you want. I can take a guess at what you're asking.

    My Crystal Ball says that you want to know how to call a constructor or something from another class inside the main method and use it to demonstrate that your program works, which you says it does. The Force has informed me that you want to call methods and stuff.


     
    public class SimpleClass
    {
     
    public static void main(String[] args)
    {
     
    YourClass yc = new YourClass();  // I don't know what your constructors look like
     
    yc.method();
     
    int variable = yc.anotherMethod();
     
    System.out.println("Variable: " + variable);
     
     
    }
    }

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java general question

    I'm guessing there is an easy way to just transfer the code . . .
    What are you talking about? Are you uncertain what a main() method is in Java, why it's important, or how to write one? I know there are some "learning" IDEs that don't require main() methods, so I could understand if that's the case here, but if your instructor is requiring one, you should know what one is. Please let us know what you need help with.

  4. #4
    Member
    Join Date
    Nov 2013
    Posts
    51
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java general question

    Quote Originally Posted by GregBrannon View Post
    What are you talking about? Are you uncertain what a main() method is in Java, why it's important, or how to write one? I know there are some "learning" IDEs that don't require main() methods, so I could understand if that's the case here, but if your instructor is requiring one, you should know what one is. Please let us know what you need help with.
    I've got a sensible question this time.

    My program contains multiple toppings, and for each topping i have the following:
     //Toppings - Egg
        System.out.print("Do you want "+egg.getType() +"?");
        input = keyboard.nextLine();
        choice = input.charAt(0);
        if (choice == 'y'){
            l.add(egg.getType());
            c.add((double) egg.getCost());
            numberOfToppings = numberOfToppings + 1;
            totalToppingPrice = totalToppingPrice + egg.getCost();
            toppings = toppings + "Egg";
                }

    I want to make this more efficient in sense so that i don't have multiple blocks of the identical code that essentially do the same job, for different toppings. Do you know how this could be done ? someone suggested using enumeration but i'm not allowed to include this.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java general question

    When you have a block of code that is being repeated multiple times, ask yourself "How can I generalize that block of code so that it can be used multiple times for different inputs." Maybe the answer is a simple method that accepts a topping and adds it, or a setter in a class that accepts a topping to be added to the item being built. In this case, you might have a Class of Toppings (I don't know what you're topping), and a Class for the object being built, like TopThis, and then as the user adds toppings to his/her TopThis object, they would be added to a collection of some kind in the TopThis object, perhaps an ArrayList<Toppings>.

    Since we don't know your level or which of these approaches you have learned or may/may not be allowed to use, it's difficult to be more specific, so some indication of the tools you're able to use would be helpful.

    Keep coding, and good luck!

  6. #6
    Member
    Join Date
    Nov 2013
    Posts
    51
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java general question

    Quote Originally Posted by GregBrannon View Post
    Keep coding, and good luck!
    PizzaTopping
    This class should model a pizza topping.

    You will need to instantiate multiple objects of each of these classes. You should have at least two PizzaBase objects (one thick, one thin), and at least four PizzaTopping objects. If you are not an expert on pizza, you can find toppings by searching for a pizza menu on the web. You will need to store this in an appropriate data structure for use within your program.

    Pay attention to the design principles that you have been taught during the module. For instance, I would expect that each ingredient will have a Cost property, which will be accessible by get/set methods, and be stored in a private member variable.

    For later stages of this coursework, you will build a simple user interface. This is a prototype system, so the user interface does NOT need to be complex; a simple command-line interface using Scanner and println statements is entirely sufficient. However, as your classes may be used in several different situations, none of Pizza, PizzaBase nor PizzaTopping should require any user interaction; use of println or Scanner within these three classes will be considered an error, and will be penalised.

    There are a number of features that you should attempt to achieve; please complete these in the order given.


    Here is my current working state of my project, but technically using only one class.


    Pizza.Java Class
     
    /*
     */
    import java.util.Scanner;
    import java.util.ArrayList;
    import java.util.List;
    import java.text.DecimalFormat;
     
    public class Pizza
    {
    	//Cost and name 
     
     
    public static void main(String [ ] args)
    {
     
    	//variables 
    	char choice;						
    	String input;						
    	String toppings = "Cheese ";	
    	double totalToppingPrice = 0.0;
    	double totalBreadPrice = 0.0;
    	int numberOfToppings = 0;	
     
    	List<String> l = new ArrayList<String>();
    	List<Double> c = new ArrayList<Double>();
     
    		//Scanner 
    		Scanner keyboard = new Scanner (System.in);	
     
    		//WELCOME 
    		System.out.println("Newcastle Pizza\n ");
     
    		//Pizza Base 
    		System.out.println("There are two pizza bases available thick or thin");
    		//Topping select/Interface
    		System.out.print("      \n**********TOPPING SELECTION***********       ");
    		String FormatTopping = " %1$-10s|%2$-7s|%3$-10s|%4$-7s|\n";
    		System.out.format(FormatTopping, "\n\n|  Bacon", "  Egg", "  Cheese ","  Prawn ");
     
     
    	//Thick base
    		System.out.print("\nDo you want thick base?");
    		input = keyboard.nextLine();
    		choice = input.charAt(0);
    		if (choice == 'y'){
    			PizzaBase thick = new PizzaBase("thick", 8.75);
    			thick.setCost(8.75);
    			thick.getType();
    			l.add(thick.getType());
    			c.add((double) thick.getCost());
    			totalBreadPrice = totalBreadPrice + thick.getCost();
    		}
     
    		//Thin base 
    		System.out.print("\nDo you want thin base?");
    		input = keyboard.nextLine();
    		choice = input.charAt(0);
    		if (choice == 'y') {
    			PizzaBase thin = new PizzaBase("thin", 7.75);
    			l.add(thin.getType());	
    			c.add((double) thin.getCost());
    			totalBreadPrice = totalBreadPrice + thin.getCost();
    		}		
     
    		//Toppings - Bacon
     
    			System.out.print("Do you want bacon?");
    			input = keyboard.nextLine();
    			choice = input.charAt(0);
    			if (choice == 'y'){
    				Toppings bacon = new Toppings();
    				bacon.setType("bacon");
    				bacon.setCost(2.50);
    			    l.add(bacon.getType());
    				c.add((double) bacon.getCost());
    				numberOfToppings = numberOfToppings + 1; 
    				totalToppingPrice = totalToppingPrice + bacon.getCost();
    				toppings = toppings + "Bacon ";
    			}
     
    			//Toppings - Cheese
    			System.out.print("Do you want cheese?");
    			input = keyboard.nextLine();
    			choice = input.charAt(0);
    			if (choice == 'y'){
    				Toppings cheese = new Toppings();
    				cheese.setType("cheese");
    				cheese.setCost(1.50);
    				l.add(cheese.getType());
    				c.add((double) cheese.getCost());
    				numberOfToppings = numberOfToppings + 1;
    				totalToppingPrice = totalToppingPrice + cheese.getCost();
    				toppings = toppings + "Cheese ";
    						}
     
    			//Toppings - Egg
    			System.out.print("Do you want egg?");
    			input = keyboard.nextLine();
    			choice = input.charAt(0);
    			if (choice == 'y'){
    				Toppings egg = new Toppings();
    				egg.setType("ham");
    				egg.setCost(1.00);
    				l.add(egg.getType());
    				c.add((double) egg.getCost());
    				numberOfToppings = numberOfToppings + 1;
    				totalToppingPrice = totalToppingPrice + egg.getCost();
    				toppings = toppings + "Egg";
    					}
     
    			//Toppings - Prawn
    			System.out.print("Do you want prawn?");
    			input = keyboard.nextLine();
    			choice = input.charAt(0);
    			if (choice == 'y' ){
    				Toppings prawn = new Toppings();
    				prawn.setType("prawn");
    				prawn.setCost(1.00);
    				l.add (prawn.getType());
    				c.add((double) prawn.getCost());
    				numberOfToppings = numberOfToppings + 1;
    				totalToppingPrice = totalToppingPrice + prawn.getCost();
    				toppings = toppings + "Prawn ";
    					}	
     
     
    			//Order confirmation 
     
    			System.out.println("Order Confirmation: ");
    			for(String s:l ){
    				System.out.println("1 x " +s );
    			}
     
    			for(double f:c){
    				//System.out.println("total cost");
    				//System.out.println("£" +f);
    				//System.out.println("GrandTotal "+GrandTotal);
    			}
    			System.out.println("Grand Total:"+ (totalBreadPrice + totalToppingPrice));
     
     
    }
     
    }

    Pizza.Base Class

     
    public class PizzaBase {
     
    	public String type;
    	public double ThickCost;
    	public double ThinCost;
    	public double cost;
     
     
     
    	public PizzaBase(String name, double cost){
    		setType(name);
    		setCost(cost);
    	}
     
     
     
     
    	public void setType(String type) {
    		this.type = type;
    	}
     
     
    	public void setCost(double cost) {
    		this.cost = cost;
    	}
     
    	public String getType() {
    		return type;
    	}
     
    	public double getCost() {
    		return cost;
    	}
     
     
     
     
     
     
     
    }



    Toppings.java Class
     
    public class Toppings {
     
    public double cost;
    public String name;
    public String type;
    public double B1Cost;
     
    public Toppings(String string, double bCost){
    // TODO Auto-generated constructor stub
    }
     
    public Toppings(String string) {
    // TODO Auto-generated constructor stub
    }
     
    public double getCost() {
    return cost;
    }
     
    public void setCost(double cost) {
    this.cost = cost;
    }
     
    public String getName() {
    return name;
    }
     
    public void setName(String name) {
    this.name = name;
    }
     
    public String getType() {
    return type;
    }
     
    public void setType(String type) {
    this.type = type;
    }
     
    public Toppings() {
    // TODO Auto-generated constructor stub
    }
     
     
     
     
     
    }

    CrustType.java class (think this in unused)
     
    public class CrustType {
     
    public CrustType(String string, int crustCost) {
    // TODO Auto-generated constructor stub
    }
    public CrustType(String string, double deepCost) {
    // TODO Auto-generated constructor stub
    }
    }



    Although this currently works, it's not in the correct format of classes. I've tried moving it about so it meets the specification, but doing it gives me many errors and not runnable.

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java general question

    It appears you've given us only part of the instructions which describes a project with multiple "stages." From what you've posted, this code isn't required to DO anything, but it is suggested to BE certain things. So, if this code is supposed to DO something, let us know what that is by posting those parts of the assignment. Which stage are you in? What are the requirements for this stage? What exactly do you need help with? And please, give us more info than "split into separate classes," because that's not very helpful. Have you already been given feedback from the instructor? If so, what was that?

  8. #8
    Member
    Join Date
    Nov 2013
    Posts
    51
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java general question

    Quote Originally Posted by GregBrannon View Post
    It appears you've given us only part of the instructions which describes a project with multiple "stages." From what you've posted, this code isn't required to DO anything, but it is suggested to BE certain things. So, if this code is supposed to DO something, let us know what that is by posting those parts of the assignment. Which stage are you in? What are the requirements for this stage? What exactly do you need help with? And please, give us more info than "split into separate classes," because that's not very helpful. Have you already been given feedback from the instructor? If so, what was that?
    Sorry, here VVCap Image

    I've near enough finished both of the above, although it works; it isn't structured ( class wise ) as the assignment states. I'm in doubt that i'll be able to get it working if i do these classes, because i can't atm. Don't know whether i'm best starting again or not.. i've received no feedback. Also, by split into seperate classes it should state in the specification, i have Pizza ( which currently includes basically everything when in reality it shouldn't contain a lot ). I'm mainly confused by if you read check the link out and read about the PizzaChoice class, my code doesn't contain what it should in there..and when i've moved it around, it would just fail completely, and i'd get many errors

  9. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java general question

    Save everything you have as it is currently, working but not in the right format.

    Create a new project (not sure what editor you're using so which terminology is correct), and copy all of your current source code files into new source code files of the same name. Close the old project - the one you have working - and make sure none of it is open so that you'll start editing it when you shouldn't be.

    When you have the old project safely tucked away somewhere, start rearranging the new project into the correct class structure based on your understanding of what you're supposed to be doing. Do the best you can, but when you get to a point where you're stuck, it won't compile and/or parts don't work, and you don't know what to do, post your code and your errors with descriptions of what you're trying to do and what you need help with.

    We'll try this for a while, and if it gets too hard or seems to be impossible, at least you'll have what you have now to fallback on.

  10. #10
    Member
    Join Date
    Nov 2013
    Posts
    51
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java general question

    Quote Originally Posted by GregBrannon View Post
    Save everything you have as it is currently, working but not in the right format.

    Create a new project (not sure what editor you're using so which terminology is correct), and copy all of your current source code files into new source code files of the same name. Close the old project - the one you have working - and make sure none of it is open so that you'll start editing it when you shouldn't be.

    When you have the old project safely tucked away somewhere, start rearranging the new project into the correct class structure based on your understanding of what you're supposed to be doing. Do the best you can, but when you get to a point where you're stuck, it won't compile and/or parts don't work, and you don't know what to do, post your code and your errors with descriptions of what you're trying to do and what you need help with.

    We'll try this for a while, and if it gets too hard or seems to be impossible, at least you'll have what you have now to fallback on.
     
     
    /*
     */
     
    import java.util.ArrayList;
    import java.util.List;
    import java.text.DecimalFormat;
     
    public class Pizza
    {
    	//Cost and name 
     
     
    	//variables 
    	char choice;						
    	String input;						
    	String toppings = "Cheese ";	
    	double totalToppingPrice = 0.0;
    	double totalBreadPrice = 0.0;
    	int numberOfToppings = 0;	
     
    	List<String> l = new ArrayList<String>();
    	List<Double> c = new ArrayList<Double>();
     
     
    PizzaBase thick = new PizzaBase("thick", 8.75);
    PizzaBase thin = new PizzaBase("thin", 7.75);
    Toppings bacon = new Toppings();
    Toppings cheese = new Toppings();
    Toppings egg = new Toppings();
    Toppings prawn = new Toppings();
     
     
     
    }
    import java.util.ArrayList;
     
    import java.util.List;
    import java.util.Scanner;
     
    import java.text.DecimalFormat;
    public class PizzaChoice {
     
    	private int IntegerparseInt(String showInputDialog) {
    		// TODO Auto-generated method stub
    		return 0;
    	}
     
    	public static void main(String [ ] args)
     
     
     
    	//variables 
    		char choice;						
    		String input;						
    		String toppings = "Cheese ";	
    		double totalToppingPrice = 0.0;
    		double totalBreadPrice = 0.0;
    		int numberOfToppings = 0;	
     
    		List<String> l = new ArrayList<String>();
    		List<Double> c = new ArrayList<Double>();
     
    			//Scanner 
    			Scanner keyboard = new Scanner (System.in);	
     
    			//WELCOME 
    			System.out.println("Newcastle Pizza\n ");
     
    			//Pizza Base 
    			System.out.println("There are two pizza bases available thick or thin");
    			//Topping select/Interface
    			System.out.print("      \n**********TOPPING SELECTION***********       ");
    			String FormatTopping = " %1$-10s|%2$-7s|%3$-10s|%4$-7s|\n";
    			System.out.format(FormatTopping, "\n\n|  Bacon", "  Egg", "  Cheese ","  Prawn ");
     
     
    		//Thick base
    			System.out.print("\nDo you want thick base?");
    			input = keyboard.nextLine();
    			choice = input.charAt(0);
    			if (choice == 'y'){
    				thick.setCost(8.75);
    				thick.getType();
    				l.add(thick.getType());
    				c.add((double) thick.getCost());
    				totalBreadPrice = totalBreadPrice + thick.getCost();
    			}
     
    			//Thin base 
    			System.out.print("\nDo you want thin base?");
    			input = keyboard.nextLine();
    			choice = input.charAt(0);
    			if (choice == 'y') {
    				l.add(thin.getType());	
    				c.add((double) thin.getCost());
    				totalBreadPrice = totalBreadPrice + thin.getCost();
    			}		
     
    			//Toppings - Bacon
     
    				System.out.print("Do you want bacon?");
    				input = keyboard.nextLine();
    				choice = input.charAt(0);
    				if (choice == 'y'){
    					bacon.setType("bacon");
    					bacon.setCost(2.50);
    				    l.add(bacon.getType());
    					c.add((double) bacon.getCost());
    					numberOfToppings = numberOfToppings + 1; 
    					totalToppingPrice = totalToppingPrice + bacon.getCost();
    					toppings = toppings + "Bacon ";
    				}
     
    				//Toppings - Cheese
    				System.out.print("Do you want cheese?");
    				input = keyboard.nextLine();
    				choice = input.charAt(0);
    				if (choice == 'y'){
    					cheese.setType("cheese");
    					cheese.setCost(1.50);
    					l.add(cheese.getType());
    					c.add((double) cheese.getCost());
    					numberOfToppings = numberOfToppings + 1;
    					totalToppingPrice = totalToppingPrice + cheese.getCost();
    					toppings = toppings + "Cheese ";
    							}
     
    				//Toppings - Egg
    				System.out.print("Do you want egg?");
    				input = keyboard.nextLine();
    				choice = input.charAt(0);
    				if (choice == 'y'){
    					egg.setType("ham");
    					egg.setCost(1.00);
    					l.add(egg.getType());
    					c.add((double) egg.getCost());
    					numberOfToppings = numberOfToppings + 1;
    					totalToppingPrice = totalToppingPrice + egg.getCost();
    					toppings = toppings + "Egg";
    						}
     
    				//Toppings - Prawn
    				System.out.print("Do you want prawn?");
    				input = keyboard.nextLine();
    				choice = input.charAt(0);
    				if (choice == 'y' ){
    					prawn.setType("prawn");
    					prawn.setCost(1.00);
    					l.add (prawn.getType());
    					c.add((double) prawn.getCost());
    					numberOfToppings = numberOfToppings + 1;
    					totalToppingPrice = totalToppingPrice + prawn.getCost();
    					toppings = toppings + "Prawn ";
    						}	
     
     
    				//Order confirmation 
     
    				System.out.println("Order Confirmation: ");
    				for(String s:l ){
    					System.out.println("1 x " +s );
    				}
     
    				for(double f:c){
    					//System.out.println("total cost");
    					//System.out.println("£" +f);
    					//System.out.println("GrandTotal "+GrandTotal);
    				}
    				System.out.println("Grand Total:"+ (totalBreadPrice + totalToppingPrice));
     
    {
     
     
     
    }
     
    }


    Hey,

    All objects created in Pizza class are receiving errors e.g. cheese, ham ectect "cheese cannot be resolved" same with thick and thin

  11. #11
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java general question

    You have no methods in your class Pizza. At a minimum, I would expect you to have a constructor. After that, the class should have some function, even if it's just to change and store the characteristics of a specific pizza object, so there will likely be methods beyond the constructor.

    Remember, think of the class as a THING with characteristics or attributes. The THING is a Pizza, its characteristics or attributes are the crust (another THING), toppings (another THING). At its most basic, a Pizza is the crust with sauce and maybe one topping, cheese. The basic, default Pizza() constructor might be the crust, the sauce, and one topping, cheese. Or it might be just the crust. You decide, write a default Pizza() constructor, and then decide which of the other items you currently have in your Pizza class should stay or go.

    Edit: As you're thinking about what should stay or go, consider what matters to the Pizza object. Does the Pizza object need to store the cost of each individual topping? No, because that should be contained in the topping object. But the Pizza object should be able to calculate how much it (the Pizza object) will cost the customer, so should be able to calculate and provide the total cost of the pizza. I think you're headed that way, and that's good, but I wanted to use that as an example of a possible thought process.

    (You don't have to quote everything I say before responding. If there's a specific point you want to reply to, quote that. Otherwise, just say what you have to say, and the thread content will provide the necessary context. If it doesn't, we'll ask.)

    Edit 2: And you'll put egg on a Pizza but no anchovies? Something's wrong with your Pizza restaurant.

Similar Threads

  1. A general question about online help
    By michael.duffy31 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 9th, 2013, 01:27 AM
  2. General question about Interfaces
    By michael.duffy31 in forum Java Native Interface
    Replies: 5
    Last Post: June 30th, 2013, 02:44 PM
  3. General Question; Need Ideas
    By clydefrog in forum Java Theory & Questions
    Replies: 2
    Last Post: March 28th, 2012, 04:42 PM
  4. General Question
    By Becca in forum Collections and Generics
    Replies: 6
    Last Post: November 3rd, 2011, 03:52 PM
  5. Just a general Java related question
    By javaisfun in forum Java Theory & Questions
    Replies: 3
    Last Post: October 25th, 2011, 07:21 AM