-2

I am new to java and cannot figure out how to work the copy constructors. Please bear with me.

I am trying to get information for shipping packages. I am trying to use the copy constructor to repeat the enter shipping details section.

I honestly have no idea what to do with it. The code works fine for one package and there are no errors - I just need to figure out how to prompt a user for a second package.

import java.util.Scanner;

public class Package {
    
    private static double length = 1.0;
    private static double width = 1.0;
    private static double height = 1.0;
        
    private static double sum1 = length+width+height;
    
        public Package(Package p) {
        this.height = p.height;
        this.length = p.length;
        this.width = p.width;
            }
    
    
    public static void main(String[] args) {
        
        Scanner input = new Scanner(System.in);
        
        System.out.println("Enter package dimensions.\nEnter Length: ");
        length = input.nextDouble(); 
    
        System.out.println("\nEnter Width: ");
        width = input.nextDouble();
    
        System.out.println("\nEnter Height: ");
        height = input.nextDouble();

    
    
        System.out.println("Package 1: " + length + " X " + width + " X " + height + ", Volume = " + sum1);
    
    }
    
}
PM 77-1
  • 12,933
  • 21
  • 68
  • 111
  • 3
    You are not even using the constructor. You can use for loop to ask user 2 or 3 or n times. – Yoshikage Kira Oct 14 '20 at 14:03
  • Bah - humiliating! They specifically asked us to use the copy constructor for the class. But I really don't even know what question I am supposed to be asking. – earwigsRgross Oct 14 '20 at 14:11
  • This honestly sounds like you should step back and read up again on what a constructor is and how they are used. – OH GOD SPIDERS Oct 14 '20 at 14:16
  • you're probably not wrong – earwigsRgross Oct 14 '20 at 14:19
  • To add to that: The accepted answer might give you the correct output on the screen, but going by your description of the assignment it is not even close to fulfilling the assignment requirements and I doubt it will be accepted as a solution. – OH GOD SPIDERS Oct 14 '20 at 14:19
  • I agree with @OHGODSPIDERS if the requirement is to use the copy constructor you need to implement it differently. You will at least have to add another constructor because now you need a Package instance to create the first object. I think this is an example of Catch-22. – Conffusion Oct 14 '20 at 14:28
  • no need to tell me - just out here doin' my best, guy. – earwigsRgross Oct 14 '20 at 14:28

2 Answers2

0

You can use do-while for this .. something like below

Scanner input = new Scanner(System.in);
    int i=0;
do{
        Package package =new Package();
    System.out.println("Enter package dimensions.\nEnter Length: ");
    package.length = input.nextDouble(); 

    System.out.println("\nEnter Width: ");
    package.width = input.nextDouble();

    System.out.println("\nEnter Height: ");
    package.height = input.nextDouble();



    System.out.println("Package : " + package.length + " X " + package.width + " X " + package.height + ", package.Volume = " + sum1);
i++;
}while ( i<=5);
Ravindra Bagale
  • 17,226
  • 9
  • 43
  • 70
0

I think your misunderstanding is in this line:

private static double sum1 = length+width+height;

This computation will be executed at the moment the runtime executes this line so when the class is loaded (so length=1.0, width=1.0, height=1.0).

If you want the sum to be calculated after you have set the length, width and height, you have to put the calculation in a method:

public static double sum() {
    return length+width+height;
}

And in your last line call this method:

System.out.println("Package 1: " + length + " X " + width + " X " + height + ", Volume = " + sum())

Also notice that all your variables are static so you don't really use the Package as an object. For simplicity you can remove the constructor except if you want to learn about java objects but then you have to adapt more code.

UPDATE: this works but to be able to use the copy constructor I each time have to create 2 objects. Speaking of overhead. Mathimatics: it is not sum but product for volumes.

import java.util.Scanner;

public class Package {

    double length=1.0;
    double height=1.0;
    double width=1.0;
    double sum;
        
    public Package() {
    }
    
    public Package (Package p)
    {
        this.length = p.length;
        this.height = p.height;
        this.width = p.width;
        // to calculate the volume it's more like a product then a sum
        sum=length*height*width;
    }

    public static void main(String[] args) {
        
        Scanner input = new Scanner(System.in);
        for (int i=0;i<5;i++)
        {
            Package pack=new Package();
            
            System.out.println("Enter package dimensions.\nEnter Length: ");
            pack.length = input.nextDouble(); 
        
            System.out.println("\nEnter Width: ");
            pack.width = input.nextDouble();
        
            System.out.println("\nEnter Height: ");
            pack.height = input.nextDouble();
            
            Package p=new Package(pack);
            System.out.println("Package 1: " + p.length + " X " + p.width + " X " + p.height + ", Volume = " + p.sum);
        }
    }
}
Conffusion
  • 4,335
  • 2
  • 16
  • 28
  • right - so maybe one of the problems was with setting it to a variable? - thank you! this helps as well! – earwigsRgross Oct 14 '20 at 14:25
  • I really appreciate this help! This is what the prof is looking for, I believe. and you're absolutely right - this was product - i wasn't paying attention. – earwigsRgross Oct 14 '20 at 15:00