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 5 of 5

Thread: constructors

  1. #1
    Member
    Join Date
    Sep 2018
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default constructors

    Hello,
    I'm very new to Java, and I'm taking a class. I'm NOT asking for someone to do my homework, I am just asking advise. We started the switch statement, and have already had a lab for constructors. For the switch statement lab, we have a user input a product number and how much is sold, then output the total sold. My code works, but I'm positive that I'm suppose to use a constructor, I'm just not sure if it's best practice to put my variables in the constructor, or maybe some other part of the code. I googled and saw that the switch statement should NOT go in the constructor. I attached my code, and someone could offer a suggestion, I would really appreciate it
    Attached Files Attached Files

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,320
    Thanks
    53
    Thanked 2,392 Times in 2,346 Posts

    Default Re: constructors

    Please post the code here in the thread. Be sure to wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2018
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: constructors

    okay, will do. By the way, I heard a description of a constructor as "it's purpose is to give some value to instance variables that are inside a class." Does that sound right?
    here is the code.
    import java.util.Scanner;
     
    public class Salesjava {
     
    	public static void main(String[] args) {
     
    		double product1;
    		double product2;
    		double product3;
    		double product4;
    		double product5;
     
    		product1 = 2.98;
    		product2 = 4.50;
    		product3 = 9.98;
    		product4 = 4.49;
    		product5 = 6.87;
     
    //		Salestest SalestestObject = new Salestest();
     
    		System.out.println("Enter a product number please ");
     
    		Scanner keyboard = new Scanner(System.in);
    		int productnumber = keyboard.nextInt();
     
    		switch (productnumber) {
     
    		case 1:
    			System.out.println("Enter the quantity sold please ");
     
    			Scanner keyboard2 = new Scanner(System.in);
    			int quantitysold = keyboard2.nextInt();
     
    			System.out.print(product1 * quantitysold);
    			break;
     
    		case 2:
    			System.out.println("Enter the quantity sold please ");
     
    			Scanner keyboard3 = new Scanner(System.in);
    			int quantitysold2 = keyboard3.nextInt();
     
    			System.out.print(product2 * quantitysold2);
    			break;
     
    		case 3:
    			System.out.println("Enter the quantity sold please ");
     
    			Scanner keyboard4 = new Scanner(System.in);
    			int quantitysold3 = keyboard4.nextInt();
     
    			System.out.print(product3 * quantitysold3);
    			break;
     
    		case 4:
    			System.out.println("Enter the quantity sold please ");
     
    			Scanner keyboard5 = new Scanner(System.in);
    			int quantitysold4 = keyboard5.nextInt();
     
    			System.out.print(product4 * quantitysold4);
    			break;
    		case 5:
    			System.out.println("Enter the quantity sold please ");
     
    			Scanner keyboard6 = new Scanner(System.in);
    			int quantitysold5 = keyboard6.nextInt();
     
    			System.out.print(product5 * quantitysold5);
    			break;
     
    		}
     
     
    	}
     
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,320
    Thanks
    53
    Thanked 2,392 Times in 2,346 Posts

    Default Re: constructors

    constructor as "it's purpose is to give some value to instance variables that are inside a class." Does that sound right?
    Yes

    Some comments:
    1)The double values can be defined and initialized on one line instead of two:
       double val;
       val = 12.3
     
       double val = 12.3;   // define and give value on one line

    2) Have a default: case at end of switch statement to handle any value not matched in any other case

    3) All the case statements have repeated code that should be done one time outside of the case:
    	System.out.println("Enter the quantity sold please ");
     
    	Scanner keyboard2 = new Scanner(System.in);
    	int quantitysold = keyboard2.nextInt();

    4) The print statements in the case should include some text describing the value being printed instead of just printing a number.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Sep 2018
    Posts
    33
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: constructors

    Thank you so much for the help!
    So I take it that I could put the variables in the constructor, and tighten up the code by putting the double values on a single line.
    I'll try that.

Similar Threads

  1. constructors
    By sumitroy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 22nd, 2014, 08:44 AM
  2. get a help about multiple constructors
    By Heshan Sandamal in forum Java Theory & Questions
    Replies: 2
    Last Post: December 1st, 2012, 02:04 PM
  3. explanatio on Constructors and others
    By OlayinkaAgboola in forum Member Introductions
    Replies: 0
    Last Post: November 1st, 2012, 08:48 AM
  4. Constructors
    By av8 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 19th, 2011, 06:40 PM
  5. [SOLVED] Overloading constructors(Multiple Constructors)
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 11th, 2011, 12:55 PM