الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

Constructors

بدأه ruka في 22 مايو 2011 · 9 رد · 594 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

Write a java class named Triangle, as shown in the UML class diagram. The class contains the following:

• 3 private attributes named side1, side2 and side3, of type (int)

•A constructor that sets the 3 sides of the triangle to given values

•A method named isValid(), which returns true if the sum of any two sides is greater than the third side.

•A method named area() which returns the area of the triangle, using the following formula:

Write another class named TestTriangle with a main() method that will create a Triangle object, initialize the data of the triangle to values entered by the user, and computes the area if the input is valid. Otherwise, it displays that the input is not valid.


private int side1;
private int side2;
private int side3;
double s;
double area;
public Triangle(int a, int b , int c){
side1=a;
side2=b;
side3=c;
}

public boolean isValid(){
if (((side1 + side2) >side3) && ((side2 + side3) > side1) && ((side3 + side1) >side2))

return true;
return false;
}

public double area(){
s=(side1+side2+side3);
area = Math.sqrt(s * (s - side1) * (s-side3)*(s-side2));
return area;
}



}



class Triangle{

import java.util.*;

public class TestTriangle{

static Scanner console=new Scanner(System.in);

public static void main(String[]args){

Triangle tr = new Triangle();

System.out.print("Enter the side1: ");

a = console.nextInt();

System.out.print("Enter the side2: ");

b = console.nextInt();

System.out.print("Enter the side3: ");

c = console.nextInt();

}

}

السؤال هو في الماين كيف انادي الكونستراكتر واخلي المستخدم يدخل القيم

#2

بدل ما تسوي

Triangle t = new Triangle();

استدعي الكونستركتور اللي ياخذ قيم بعد ما المستخدم يدخلها

يعني

a= ...
b= ...
c= ...
Triangle t = new Triangle(a,b,c);

طبعا لاحظي انكي مادام عملتي overloading للكونستركتور ، استدعاء الdefault constructor راح يديكي خطأ

يعني الكود الاول

Triangle t= new Triangle();

غير صحيح

No intellectual battle was ever won through retreat
You do not watch Gintama? Dude, you are missing a lot!


صورةmsrgb1485.gif ocajavase7programmerclr.gif

#3

:blush: لم افهم قصدك اخي

#4

ما يقصد mental-driller انه اذا عملت كوسنتراكتر (طبعا هو مثيود عادي ) يستدعى بشكل افتراضي عند انشاء الكائن (object ) , ما دام انك سويت كونستراكتر به ثلاثة باراميتر اللي هم قيم ل slides فلازم انك تضعهم عند انشاء الابوجكت لانه يستدعى بشكل تلقائي

فضع القيم اللي يدخلهم المستخدم في سطر تعريف و انشاء الاوبجكت , بين القوسين

 System.out.print("Enter the side1: "); 
a = console.nextInt(); 
System.out.print("Enter the side2: "); 
b = console.nextInt(); 
System.out.print("Enter the side3: "); 
c = console.nextInt();

Triangle tr = new Triangle(a,b,c);

علما ان كتابة كونستراكتر لادخال البيانات في هذه الحالة لا فائدة منها لان المستخدم هو من يدخل البيانات و ليس المبرمج , و استاذك طلب منك هذا الشي ليتأكد انك فاهم الكوستراكتر

تم تعديل هذه المشاركة بواسطة Game111 في 22 مايو 2011 في 21:13

#5

اهلن :wub: تم الحل بس مدري اذا صح ولا غلط بس طلع معي كل شي

class Triangle{
private int side1;
private int side2;
private int side3;
double s;
double area;
public Triangle(int a, int b , int c){
side1=a;
side2=b;
side3=c;
}

public boolean isValid(){
if (((side1 + side2) >side3) && ((side2 + side3) > side1) && ((side3 + side1) >side2)) 

return true;
return false;
}

public double area(){
s=(side1+side2+side3);
area = Math.sqrt(s * (s - side1) * (s-side3)*(s-side2)); 
return area;
}



}

import java.util.*;
public class TestTriangle{
static Scanner console=new Scanner(System.in);
public static void main(String[]args){
boolean y;
double m;
int z,w,x;
System.out.print("Enter the side1: "); 
z = console.nextInt(); 
System.out.print("Enter the side2: "); 
w = console.nextInt(); 
System.out.print("Enter the side3: "); 
x = console.nextInt(); 
Triangle tr = new Triangle(z,w,x);
y=tr.isValid();

m=tr.area();
if (y==true){
System.out.println("the area is: " + m);
}
if(y==false){
System.out.println(z + w + x);
}
}

}
#6

الحمد لله

No intellectual battle was ever won through retreat
You do not watch Gintama? Dude, you are missing a lot!


صورةmsrgb1485.gif ocajavase7programmerclr.gif

#7
ruka كتب:

اهلن :wub: تم الحل بس مدري اذا صح ولا غلط بس طلع معي كل شي

class Triangle{
private int side1;
private int side2;
private int side3;
double s;
double area;
public Triangle(int a, int b , int c){
side1=a;
side2=b;
side3=c;
}

public boolean isValid(){
if (((side1 + side2) >side3) && ((side2 + side3) > side1) && ((side3 + side1) >side2))

return true;
return false;
}

public double area(){
s=(side1+side2+side3);
area = Math.sqrt(s * (s - side1) * (s-side3)*(s-side2)); 
return area;
}



}

على حسب السؤال من المفترض يكون للي لونه احمر

 if (((side1 + side2) >side3) || ((side2 + side3) > side1) || ((side3 + side1) >side2))

الخطأ الثاني كاتب في اخر الميثود

 return true; 
return false;

في هذا الحالة لو تحقق الشرط راح يرجع true و false و ممكن يأثر على القيمة الحقيقية ,, من المفترض انك تستعمل if - else

import java.util.*;
public class TestTriangle{
static Scanner console=new Scanner(System.in);
public static void main(String[]args){
boolean y;
double m;
int z,w,x;
System.out.print("Enter the side1: "); 
z = console.nextInt(); 
System.out.print("Enter the side2: "); 
w = console.nextInt(); 
System.out.print("Enter the side3: "); 
x = console.nextInt(); 
Triangle tr = new Triangle(z,w,x);
y=tr.isValid();

m=tr.area();
if (y==true){
System.out.println("the area is: " + m);
}
if(y==false){
System.out.println(z + w + x);
}
}

}

و هنا ايضا خطأ

استعمل if-else بدل ما تستعمل if مرتين لانك قاعد تقارن لنفس القيمة في حالتان اما صح او خطأ ,, فاذا هي صح عسكها بيكون خطأ

if(y==true)
 else ........
  .......

تم تعديل هذه المشاركة بواسطة Game111 في 23 مايو 2011 في 00:17

#8

بالنسبة if in the main وضع الاقواس اتوقع يغني عن وضع else ولا لا

#9
ruka كتب:

بالنسبة if in the main وضع الاقواس اتوقع يغني عن وضع else ولا لا

الاقواس لا تعني شي سوى انها تحزم الجمل داخل اطار واحد

لو طلب منك استاذك بكتابة برنامج بقوم بتعريف عشرة متغيرات (a,b,c,d,e,f,g,h,i,j) من نوع عدد صحيح ذات قيم عشوائية و يقرر ما اذا كان المتغير a اكبر عدد او لا , كيف ستكتب البرنامج ؟؟

ها هو البرنامج

 import java.util.Random;


/**
 *
 * @author NEW
 */
public class NewClass1 {
public static void main(String[]args)
    {
    int a,b,c,d,e,f,g,h,i,j;

    Random r=new Random();
   a=r.nextInt(); b=r.nextInt(); c=r.nextInt();  d=r.nextInt(); e=r.nextInt(); f=r.nextInt();
     g=r.nextInt(); h=r.nextInt(); i=r.nextInt();  j=r.nextInt();

   if(a>b&&a>c&&a>d&&a>e&&a>f&&a>g&&a>h&&a>i&&a>j)
      System.out.println("a is the maximum");

   else 
       System.out.println("a is not the maximum");


}
}

لكن انا اريدك ان تكتبه ب if فقط من دون استعمال else

أرني !

:wink:

تم تعديل هذه المشاركة بواسطة Game111 في 23 مايو 2011 في 22:35

#10

عذرا على التاخير كان لدي اختبارات في الفتره الماضيه الشهريه :sleep:

عملت ميثود ترجع قيم صائب وخاطئ

import java.util.*;
public class m{
static Scanner console=new Scanner(System.in);
public static void main(String[]args){
int num1,num2,num3,num4,num5,num6,num7,num8,num9,num10;
System.out.println("Enter the number's");
num1=console.nextInt(); 
num2=console.nextInt(); 
num3=console.nextInt(); 
num4=console.nextInt(); 
num5=console.nextInt(); 
num6=console.nextInt(); 
num7=console.nextInt();
num8=console.nextInt();
num9=console.nextInt();
num10=console.nextInt(); 
if(islarge(num1,num2,num3,num4,num5,num6,num7,num8,num9,num10)) 
System.out.println("TRUE a is the maximum");
System.out.println("FALSE");

}
public static boolean islarge(int a,int b,int c,int d,int e,int f,int g,int h,int i,int j){

return (a>b&&a>c&&a>d&&a>e&&a>f&&a>g&&a>h&&a>i&&a>j);



}










}

مواضيع مشابهة