Skocz do zawartości

Pole i objętość prostopadłościanu w Javie.


Recommended Posts

Napisano

Witam! Potrzebuje kod na program w javie, aby liczył pole powierzchni i objętość prostopadłościanu.

Chodzi o to, że my podajemy boki, a program wylicza i wyświetla pole i objętość.

Z góry wielkie dzięki.

  • 4 weeks later...
Napisano

public class Prostopadloscian {

private int bokA;

private int bokB;

private int bokC;

public Prostopadloscian(int bokA, int bokB, int bokC) {

this.bokA = bokA;

this.bokB = bokB;

this.bokC = bokC;

}

public void pole() {

int s = 2 * ((bokA * bokB) + (bokB *bokC) + (bokA * bokC));

System.out.println("Pole powierzchni = " + s);

}

public void objetosc() {

int v = bokA * bokB * bokC;

System.out.println("Objetosc = " + v);

}

public static void main(String[] args) {

Prostopadloscian figura = new Prostopadloscian(3,3,3);

figura.pole();

figura.objetosc();

}

}

 

Napisano

Nie uczmy złych nawyków (czepiam się). Pozwolę sobie poprawić ten kod:

 

public class Prostopadloscian {

private double bokA;

private double bokB;

private double bokC;

public Prostopadloscian(double bokA, double bokB, double bokC) {

this.bokA = bokA;

this.bokB = bokB;

this.bokC = bokC;

}

public double pole() {

return 2 * ((bokA * bokB) + (bokB *bokC) + (bokA * bokC));

}

public double objetosc() {

return bokA * bokB * bokC;

}

public static void main(String[] args) {

Prostopadloscian figura = new Prostopadloscian(3,3,3);

System.out.printf("Pole wynosi %f\n", figura.pole());

System.out.printf("Objętość wynosi %f\n", figura.objetosc());

}

}

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Gość
Odpowiedz...

×   Wkleiłeś zawartość bez formatowania.   Usuń formatowanie

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Utwórz nowe...