0

I have a problem with the following code in java.

I have to have two constructors where in one I have to default to yellow and the other one you should be able to choose. what am I doing wrong?

secondly the format to one decimal in method beraknaomkrets doesnt seem to work, what am I doing wrong.

import java.text.*;
import java.io.*;
import java.util.*;

public class Cirkel
{
    //instansvariabler
    private int radie;
    private String farg;

    //konstruktorn
    public Cirkel()
    {
        radie = 0;
        farg = "";
    }

    public Cirkel(String farg)
    {
        radie = 0;
        farg = "gul";
    }

    //metod där man får ange färg
    public void andraFarg()throws IOException
    {
        BufferedReader myIn = new BufferedReader
            (new InputStreamReader (System.in));

        System.out.print("Ange vilken färg cirkeln ska ha: ");
        farg = myIn.readLine();
    }

    //metod som returnerar den aktuella färgen på cirkeln
    public String hamtaFarg()
    {
        return farg;
    }

    //metod där man får ange radien på cirkeln
    public void andraRadie()throws IOException
    {
        BufferedReader stdin = new BufferedReader
            (new InputStreamReader (System.in));

        System.out.print("Välj vilken radie cirkeln ska ha: ");
        radie = Integer.parseInt(stdin.readLine());
    }

    //metod som returnerar radien
    public int hamtaRadie()
    {
        return radie;
    }

    public double beraknaOmkrets()
    {
        NumberFormat formatter = new DecimalFormat("#0.0");

        double omkrets = 0;
        omkrets = radie*2*3.14;
        formatter.format(omkrets);
        return omkrets;
    }

    public double beraknaArea()
    {
        NumberFormat formatter = new DecimalFormat("#0.0");
        double area = 0;
        area = radie*radie*3.14;
        formatter.format(area);

        return area;
    }
}

class TestCirkel
{
    public static void main(String args[])throws IOException
    {
        BufferedReader myIn = new BufferedReader
        (new InputStreamReader (System.in));

        Cirkel c1 = new Cirkel(); //skapa objektet c1
        Cirkel c2 = new Cirkel("gul");

        c1.andraFarg();
        c1.andraRadie();
        System.out.println(" Färgen på cirkel 1 är "+ c1.hamtaFarg());
        System.out.println(" Omkretsen på cirkel 1 är "+ c1.beraknaOmkrets());
        System.out.println("Arean på cirkel 1 är "+c1.beraknaArea());

        c2.andraRadie();
        System.out.println(" Färgen på cirkel 2 är "+c2.hamtaFarg());
        System.out.println(" Omkretsen på cirkel 2 är "+c2.beraknaOmkrets());
        System.out.println("Arean på cirkel 2 är "+c2.beraknaArea());
    }
}
Jason Orendorff
  • 42,793
  • 6
  • 62
  • 96
user1534779
  • 51
  • 1
  • 2
  • 6

3 Answers3

3

This will assign the value of variable farg from the paramater, to the instance variable farg.

public Cirkel(String farg)
{
    radie = 0;
    this.farg = farg;
}

Now, if your desire is to set the value of farg to "gul" no matter what then you have two options. Initialize the instance variable as gul

private String farg = "gul";

or make a modification to your default constructor. Either one will work for you.

public Cirkel()
{
    radie = 0;
    this.farg = "gul";
}
gmustudent
  • 2,229
  • 6
  • 31
  • 43
  • well where the default is gul, it throws an exception null. – user1534779 Aug 01 '12 at 18:27
  • @OP: In that constructor, `farg = "gul"` does not set the instance variable `farg`, it sets the local variable `farg`, because the local variable is closer in scope, hiding the instance variable. `this.farg` refers to the instance variable. – Matt Aug 01 '12 at 18:31
0

In your non-default constructor you are just setting the farg value to "gul" rather than using the passed in value. That constructor should be using

this.farg = farg

Also try using DecimalFormat for your formatting method as found here

Community
  • 1
  • 1
Raskolnikov
  • 286
  • 3
  • 13
  • There isn't anything wrong with his multiplication. int*double is automatically cast to a double. – namenamename Aug 01 '12 at 18:31
  • I have the following code but it is just not formatting public double beraknaArea() { DecimalFormat formatter = new DecimalFormat("#0.0"); double area = 0; area = radie*radie*3.14; formatter.format(area); return area; – user1534779 Aug 01 '12 at 18:46
0

You're receiving the value in a constructor, but you're not setting it to the class variable. In the default constructor the value is set to an empty string, it should be set to the color you are interested in ("gul").

Shashank Agarwal
  • 2,769
  • 1
  • 22
  • 24