Ich bin auf eine Frage gestoßen, bei der wir das Volumen einer Kugel, eines Zylinders und eines Kegels mit Methodenüberladung berechnen müssen. Im Folgendes ist mein Code, die absolut in Ordnung ist:Anzahl der während der Überladung der Java-Methode zusammenfallenden Argumente
import java.util.*;
class Vol_Sph_Cyl_Con
{
void calc_volume(double sp_rd)
{
double volume=(4*3.14*(Math.pow(sp_rd,3)))/3;
System.out.println("Volume of the sphere is "+volume+" cc");
}
void calc_volume(double cyl_rd, double cyl_he)
{
double volume=3.14*(Math.pow(cyl_rd,2))*cyl_he;
System.out.println("Volume of the cylinder is "+volume+" cc");
}
void calc_volume(double con_rd,double con_he,double pie)
{
double volume=(pie*(Math.pow(con_rd,2))*con_he)/2;
System.out.println("Volume of the cone is "+volume+" cc");
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
Vol_Sph_Cyl_Con ob=new Vol_Sph_Cyl_Con();
double sp_rad=0.0;
do
{
System.out.println("Enter radius of sphere to calculate the volume (in centimetres)");
sp_rad=sc.nextDouble();
if (sp_rad<=0)
{
System.out.println("Error!");
}
}while (sp_rad<=0);
ob.calc_volume(sp_rad);
double cyl_rad=0.0, cyl_hei=0.0;
do
{
System.out.println("Enter the radius and the height of cylinder respectively to calculate volume (in centimetres)");
cyl_rad=sc.nextDouble();
cyl_hei=sc.nextDouble();
if (cyl_rad<=0 || cyl_hei<=0)
{
System.out.println("Error!");
}
}while (cyl_rad<=0 || cyl_hei<=0);
ob.calc_volume(cyl_rad,cyl_hei);
double con_rad=0.0, con_hei=0.0, pi=3.14;
do
{
System.out.println("Enter radius and height of cone respectively to calculate volume (in centimetres)");
con_rad=sc.nextDouble();
con_hei=sc.nextDouble();
if (con_rad<=0 || con_hei<=0)
{
System.out.println("Error!");
}
}while (con_rad<=0 || con_hei<=0);
ob.calc_volume(con_rad,con_hei,pi);
}
}
Das Programm ist absolut in Ordnung, aber das Problem ist, wie dies für ein Schulprojekt ist, dass ich nicht das Argument ‚pie‘ in der Methode übergeben kann, die den Wert berechnet von Kegel, da wir nicht explizit etwas passieren dürfen, was nicht erforderlich ist. Aber wenn ich das nicht tue, fällt die Anzahl der Argumente zusammen. Gibt es einen Weg, dies zu lösen, oder ist es nur ein Problem mit der Frage?
@mikeTheLiar weil wir die Aufgabe mit nur Methode Überlastung –
@ShinjineeMaiti hoppla tun sollen, sorry. Ich schätze, ich kann heute nicht lesen. – MikeTheLiar
@Zircon meinst du den zweiten Satz? –