Jump to content

Please help me out with these severals questions

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
farhanyun91

farhanyun91

    Learning Programmer

  • Members
  • PipPipPip
  • 53 posts
i have a couple of questions that need to be answered

1)i cant identify the error for this code.
public CarGetNSet

{

    {

        public static void main(String []args)

        {

            Car car = new Car();

            car.setModel("BMW");

            car.setColor("Red");

            car.set(4000.78);

            System.out.println("The car color is " + car.getColor());

            System.out.printl("The car model is " + car.getModel());

            System.out.println("The car mileage is " + car.getMileage());

        }

        public Class Car

        {

            private String color;

            private String model;

            private double mileage;


            public void setColor(String color)

            {


                this.color = color;

            }


            public String getColor()

            {

                return color;

            }


            public void setModel(String model)

            {

                this.model = model;

            }


            public String getModel()

            {

                return model;

            }


            public void setMileage(double mileage)

            {

                this.mileage = mileage;

            }


            public double getMileage()

            {

                return mileage;

            }


        }

    }

}


2)having the same problem as number 1
import javax.swing JOptionPane;

public WolfGoatCabbageRiddle

{

    public static void main(String [] args)

    {

        String str;

        String animal;


        System.out.println("A farmer needs to move a wolf, a goat and a cabbage across a river.The boat is");

        System.out.println("tiny and can only carry one passenger at a time.If he leaves the wolf and the goat");

        System.out.println("alone together,the wolf will eat the goat.If he leaves the goat and the cabbage together,");

        System.out.println("the goat will eat the cabbage.");


        JOptionPane.showMessageDialog(null,"How can he brings all three safely across the river?", "Question",JOptionPane.Question_Message);


        str=JOptionPane.showInputDialog("Which one of these three shall he picks first?","Answer");

        animal=String.parseString(str);


        if (animal=wolf)

            JOptionPane.showDialogMessage(null,"GOAT WILL EAT THE CABBAGE.THEREFORE,YOU HAVE FAILED TO BRING ALL THREE SAFELY!!!!");

            else

            if (animal=cabbage)

                JOptionPane.showDialogMessage(null,"WOLF WILL EAT THE GOAT.THEREFORE,YOU HAVE FAILED TO BRING ALL THREE SAFELY!!!!");

                else

                    JOptionPane.showDialogMessage(null,"YOU HAVE MANAGE TO BRING ALL THESE THREE ACROSS THE RIVES SAFELY.THEREFORE,MISSION ACCOMPLISH!!!!!");

    }

}


3)


public TestEmployeeGetNSet

{

    public static void main(String [] args)

    {

        Employee Employee1=new Employee

        Employee1.setEmpNo(40.0);

        Employee1.setLastName("Abu");

        System.out.println(Employee1.getEmpno());

        System.out.println(Employee1.getLastName());

    }

    public class Employee

    {

        private int empNo;

        private String lastName;


        public int getEmpNo()

        {

            return empNo;

        }

        public void setEmpNo(int newEmpNo)

        {

            empNo=newEmpNo;

        }


        public String getLastName()

        {

            return lastName;

        }

        public void setLastName(String newLastName)

        {

            lastName=newLastName;

        }


    }

}


4)is it possible if i want user to key in input for the case get and set method??how can i do it?Please help me out.Appreciate it.thanks

Edited by farhanyun91, 25 August 2010 - 10:13 AM.


#2
Roman Y

Roman Y

    Programmer

  • Members
  • PipPipPipPip
  • 189 posts
please rewrite your code within code tags (button on the panel looks like this: #) and use the indention pls, so it's not a pain in the ass for people to read the code.

#3
Roman Y

Roman Y

    Programmer

  • Members
  • PipPipPipPip
  • 189 posts
P.S for us to test your code we also might need to see the classes Employee and Car, because there could be something wrong there...

#4
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
1)
public CarGetNSet
{
What is "Car get and set"? Name it to "CarDemo" or something. Also, it's not a good idea to wrap it around your Car class.
Because this is actually a class, it should be declared as one: "public class CarDemo"
PS. Your car doesn't have a method "set(int arg)", which you are trying to call from the main method.

2)
animal=String.parseString(str);
str is a string. no need to parse anything, lose the str variable and assign the String object directly from the JOptionPane to the animal variable.
animal = JOptionPane.showInputDialog("Which one of these three shall he picks first?","Answer");
if (animal=wolf)
            JOptionPane.showDialogMessage(null,"GOAT WILL EAT THE CABBAGE.THEREFORE,YOU HAVE FAILED TO BRING ALL THREE SAFELY!!!!");
            else
            if (animal=cabbage)
                JOptionPane.showDialogMessage(null,"WOLF WILL EAT THE GOAT.THEREFORE,YOU HAVE FAILED TO BRING ALL THREE SAFELY!!!!");
                else
                    JOptionPane.showDialogMessage(null,"YOU HAVE MANAGE TO BRING ALL THESE THREE ACROSS THE RIVES SAFELY.THEREFORE,MISSION ACCOMPLISH!!!!!");
if (animal=cabbage)

(PS single equation mark means assigning not comparison!)
Java: ==, .equals(), compareTo(), and compare()

String comparison is done by using "equals" method, and String itself is surrounded by double quotes, if no variable is used.
if(animal.equals("cabbage"))
3)

Employee Employee1=new Employee
Huh? This code is just unfinished. Did you even try to search the error for yourself, before posting it here?
Employee Employee1=new Employee();

Employee1.setEmpNo(40.0);
Your Employee class doesn't have a method setEmpNo(float arg), nor setEmpNo(double arg). So lose the dot from the 40.0 and write it as an integer. (40)

#5
Roman Y

Roman Y

    Programmer

  • Members
  • PipPipPipPip
  • 189 posts
dude I've went through your code and I'm sorry to say that it is really bad... besides many syntax errors (do you use any Java Development Kit such as "Eclipse Galileo"). you're nesting classes inside of eachother and to be able to call them from the class that has a main method you have to make the inner classes static which of course leaves it pointless to create a accessors and mutators alltogether because you can change the value of the instance variables just by typing car.color = "red"; so I'd strongly advice you not to nest those classes.

#6
farhanyun91

farhanyun91

    Learning Programmer

  • Members
  • PipPipPip
  • 53 posts
is it possible if you show me the right code??i have nobody to correct me.im learning programming all alone.so would you mind??so that i can refer to your coding

#7
Roman Y

Roman Y

    Programmer

  • Members
  • PipPipPipPip
  • 189 posts
Sure thing. In first one all you really need to do is to separate two classes into two different files:
CarGetNSet.java:
public class CarGetNSet

{

    {

        public static void main(String[] args)

        {

            Car car = new Car();

            car.setModel("BMW");

            car.setColor("Red");

            car.setMileage(4000.78);

            System.out.println("The car color is " + car.getColor());

            System.out.printl("The car model is " + car.getModel());

            System.out.println("The car mileage is " + car.getMileage());

        }

}


and
Car.java
public class Car

        {

            private String color;

            private String model;

            private double mileage;


            public void setColor(String color)

            {


                this.color = color;

            }


            public String getColor()

            {

                return color;

            }


            public void setModel(String model)

            {

                this.model = model;

            }


            public String getModel()

            {

                return model;

            }


            public void setMileage(double mileage)

            {

                this.mileage = mileage;

            }


            public double getMileage()

            {

                return mileage;

            }


        }


In the second one you have missspelled a couple of things like you've once again ommited keyword class, swapt Message and Dialog... but that's small stuff here's the complete code:
WolfGoatCabbageRiddle.java
import javax.swing.JOptionPane;

public class WolfGoatCabbageRiddle

{

    public static void main(String [] args)

    {

        String str;

        String animal;


        System.out.println("A farmer needs to move a wolf, a goat and a cabbage across a river.The boat is");

        System.out.println("tiny and can only carry one passenger at a time.If he leaves the wolf and the goat");

        System.out.println("alone together,the wolf will eat the goat.If he leaves the goat and the cabbage together,");

        System.out.println("the goat will eat the cabbage.");


        JOptionPane.showMessageDialog(null,"How can he brings all three safely across the river?", "Question",JOptionPane.QUESTION_MESSAGE);


        str=JOptionPane.showInputDialog("Which one of these three shall he picks first?","Answer");

        animal=str;


        if (animal.equalsIgnoreCase("wolf"))

            JOptionPane.showMessageDialog(null,"GOAT WILL EAT THE CABBAGE.THEREFORE,YOU HAVE FAILED TO BRING ALL THREE SAFELY!!!!");

            else

            if (animal.equalsIgnoreCase("cabbage"))

                JOptionPane.showMessageDialog(null,"WOLF WILL EAT THE GOAT.THEREFORE,YOU HAVE FAILED TO BRING ALL THREE SAFELY!!!!");

                else

                    JOptionPane.showMessageDialog(null,"YOU HAVE MANAGE TO BRING ALL THESE THREE ACROSS THE RIVES SAFELY.THEREFORE,MISSION ACCOMPLISH!!!!!");

    }

}

the one fatal misstake you made in that code was: well actually there was two errors one is syntax the other one is logical, here's the explanetion for both:

you wrote if(animal = wolf) first one.. you probably know that if needs a boolean expression as a parameter and a single = (assignment operator) that is animal gets value of wolf. What you probably meant was == (equals operator) that is if animal equals to wolf which returns a boolean expression. That's when you run into the second logical error... you see Strings is an object type which means it is not stored the same way as a primitive type such as integer or double. The variables don't actually have the value of the string, just an adress that points to a piece of memory that contains the String so when you write if(animal == wolf) you're asking to evaluate if those two strings point to the same adress which they most likely don't (unless someone assigned to the same adress before which in this case no one did) what you want to do is to check if the have same value and String has two methods that can do that: equals(Object) which is case sensitive and equalsIgnoreCase(String) which is not case sensitive, and you always want to use method equals(Object) that all objects have by default if you want to check if values of two objects are equal.

And finally in the third one same problem as in the first... two files needs to be created for each class:

TestEmployeeGetNSet.java:
public class TestEmployeeGetNSet

{

    public static void main(String [] args)

    {

        Employee Employee1 = new Employee();

        Employee1.setEmpNo(40);

        Employee1.setLastName("Abu");

        System.out.println(Employee1.getEmpNo());

        System.out.println(Employee1.getLastName());

    }

}

Employee.java:
public class Employee 

{

	private int empNo;

	private String lastName;


	public int getEmpNo() 

	{

		return empNo;

	}


	public void setEmpNo(int newEmpNo) 

	{

		empNo = newEmpNo;

	}


	public String getLastName() 

	{

		return lastName;

	}


	public void setLastName(String newLastName) 

	{

		lastName = newLastName;

	}


}

So there is one particular error I've noticed that you did. It was with the setEmpNo(int newEmptNo). So you've created a method that takes an int as an argument and then you give it parameter 40.0 which is a floating point number. Now to explain why that is a problem. Yes Java can automaticly do a simple type cast for you but type casts arn't alway avalible just like that. You can only cast type to a type that is "bigger" which means that it can use more bits to represent the number so it can go byte -> int -> long -> float -> double but under no conditione there can be a type cast the other way around. which means that you should only type 40 as a parameter, because with a dot it outomaticly becomes a double and java is not able to type cast it by itself. Other than that there was nothing major.

#8
farhanyun91

farhanyun91

    Learning Programmer

  • Members
  • PipPipPip
  • 53 posts
oookkkaayyy
the bottom line now is,to do the mutator and accessor , we do need to create two separated classes ,don't we??
the first one with the data field and the another one with invoke implementation,correct me if i wrong?

and if we want to do constructor, there is no need to do two separated classes,am i correct??

#9
Roman Y

Roman Y

    Programmer

  • Members
  • PipPipPipPip
  • 189 posts
Not really... you create a class for every object and it's simplier to create a class in a separate file, there could be exceptions ofcourse if you say making a listner class for a button but that's not the case... you need a class where you have the main method and create other objects to do something, like here you've created an object employee. It just so happened that your employee didn't have any other methods than accessors and mutators, but it could have methods like work() or takeLunch().

Let's say you have a class Employee(int ID, String Name) and let's say that employee has methods work(), takeLunch(), takeVacation(), workOvertime(int hours) (accessors and mutators should be written as a default to every class you write (if you don't other classes to be able to "change" an employee you make mutator a private method) as well as method toString()) so anyway now let's say you want to create a workstation. So you create a class that you call WorkStation where you create a bunch of objects of type Employee and in that class you have methods setSchedule(int start, int end, int daysAweek, int ID) and a bunch of other methods so you can set up a proper work station with as many employees as you want, and you may have a method that starts this whole thing going that's called start()
Now you need to create a main class that has the main method and the only thing you do there is create the object of WorkStation and command it to start, the rest is incapsuled because you abstract all of the other tings.

I haven't really tryed to explain how object oriented programming works before, I hope you get some idea. If not please ask and I'll try to explain better.