HelpNeed urgent help!

Posts 1–5 of 5 · Page 1 of 1
Need urgent help!
so i have an assignment due in the next 19 hours and im pretty basic with java...here is my assingment

Create any class that you want that represents any real world object. It must have the following

At least 3 instance variables (each one must be a different data type) (1pt)
getter methods (accessor) for each instance variable (1pt)
setter methods (mutator) for each instance variable (1pt)
a display() method that prints out all the values of your instance variables (1pt)
Create another java file called Demo.java , and in the main method create 2 objects of your class. Set the all the data of each object using your setter methods, and call the display() method for each object (1pt)

i have chosen to do a bag of chips, so my 3 instances would be height,length and area...im not too comfortable with getters and setters
anything helps...thank you for your time in advance
I believe that getter and setter are just inputs from a keyboard .

I always used the Keyboard Class or the scan method .
For the Keyboard Class you only need to do a new file and I'm sure you can find the whole code on the internet .

Than you just do something like this:
Code:
example = Keyboard.readInt () ;
All of this is at the top of my head. Do ask me if you think you have found a mistake.

Code:
// CLASS 1
public class BagOfChips 
{
        private double height; // 0.00
        private int width; // 0
        private String type; //Salty, paprika, ...

        //display() displays all the values of the variables in the console
        public void display()
        {
                System.out.println("Details of BagOfChips:");
                System.out.printf("Height variable value: %.2f\n", this.height);
                System.out.printf("Width variable value: %d\n", this.width);
                System.out.printf("Type variable value: %s\n", this.type);
        }

        //getter height
        public double getHeight()
        {
                return this.height;
        }

        //setter height
        public void setHeight(double height)
        {
                this.height = height;
        }

        //getter width
        public int getWidth()
        {
                return this.width;
        }

        //setter width
        public void setWidth(int width)
        {
                this.width = width;
        }

        //getter type
        public String getType()
        {
                return this.type;
        }
        
        //setter type
        public void setType(String type)
        {
                this.type = type;
        }

}

//CLASS 2

public class Demo
{
        public static void main(String[] args)
        {
                BagOfChips bagOfChip1 = new BagOfChips();
                BagOfChips bagOfChip2 = new BagOfChips();
                
                //set values of the first BagOfChips
                bagOfChip1.setType("Salty");
                bagOfChip1.setHeight(5.00); //.00 isn't needed, but it's just to show you it can take values beyond the '.'
                bagOfChip1.setWidth(3); //This can't take values beyond the .00 if you would do 3.99 it would just be 3

                //Set the values of the second bag of chips
                bagOfChip2.setType("Paprika");
                bagOfChip2.setHeight(10.00); 
                bagOfChip2.setWidth(6);
                
                bagOfChip1.display();
                bagOfChip2.display();                
                

        }
}
Quote Originally Posted by Erinador View Post
All of this is at the top of my head. Do ask me if you think you have found a mistake.

Code:
// CLASS 1
public class BagOfChips 
{
        private double height; // 0.00
        private int width; // 0
        private String type; //Salty, paprika, ...

        //display() displays all the values of the variables in the console
        public void display()
        {
                System.out.println("Details of BagOfChips:");
                System.out.printf("Height variable value: %.2f\n", this.height);
                System.out.printf("Width variable value: %d\n", this.width);
                System.out.printf("Type variable value: %s\n", this.type);
        }

        //getter height
        public double getHeight()
        {
                return this.height;
        }

        //setter height
        public void setHeight(double height)
        {
                this.height = height;
        }

        //getter width
        public int getWidth()
        {
                return this.width;
        }

        //setter width
        public void setWidth(int width)
        {
                this.width = width;
        }

        //getter type
        public String getType()
        {
                return this.type;
        }
        
        //setter type
        public void setType(String type)
        {
                this.type = type;
        }

}

//CLASS 2

public class Demo
{
        public static void main(String[] args)
        {
                BagOfChips bagOfChip1 = new BagOfChips();
                BagOfChips bagOfChip2 = new BagOfChips();
                
                //set values of the first BagOfChips
                bagOfChip1.setType("Salty");
                bagOfChip1.setHeight(5.00); //.00 isn't needed, but it's just to show you it can take values beyond the '.'
                bagOfChip1.setWidth(3); //This can't take values beyond the .00 if you would do 3.99 it would just be 3

                //Set the values of the second bag of chips
                bagOfChip2.setType("Paprika");
                bagOfChip2.setHeight(10.00); 
                bagOfChip2.setWidth(6);
                
                bagOfChip1.display();
                bagOfChip2.display();                
                

        }
}
, Either YOU uploaded this source or it's not off the top of your head as you claim.
I swear to god, it's all typed in the Quick reply box. (Used multiple spaces as tabs)

I'm a college student so this stuff is easy peasy lemon squeezy.

I guess I could take it as a complement. =D
Posts 1–5 of 5 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us