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

Get And Set Methods

بدأه PROGRAMMER_15 في 31 يناير 2009 · 6 رد · 840 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

لدي واجب يجب أن أحله وهذا نص السؤال:

II) Design and implement selected parts of a simple inventory control system using the following specifications.

1) Implement a class to represent the information about each part in the inventory. Each part (for now – this might change for later assignments) has only three pieces of information associated with it: an ID (a string), a description (a string with potentially more than one word), and the quantity in stock. You must provide the following public interface:

a) Set and Get methods for each of these three pieces of information

B) A no-argument constructor which initializes a part’s ID to “0000”, its description to “No part information”, and its quantity to 0.

c) A constructor with three parameters (one for ID, one for description, and one for quantity), which initializes the ID, description, and quantity to the three parameters passed to it.

d) A display method which prints the part information to the screen in one line.

محاولتي هي:

public class inventory {



   protected String ID;
   protected String Description;
   protected String Quantity;

   // Constructor
   public inventory(String newID,String newDescription,String newQuantity)
   {
	   ID=newID;
	   Description=newDescription;
	   Quantity=newQuantity;

   }
   // Set and Get Methods
   public String getID()
   {
return ID;
   }
   public void setID(String newID)
   {
		   ID=newID;
   }
   public String getDescription()
   {
	   return Description;
   }
   public void setDescription(String newDescription)
   {
	   Description=newDescription;
   }

   public String getQuantity()
   {
	   return Quantity;
   }
   public void setQuantity(String newQuantity)
   {
	   Quantity=newQuantity;
   }

}

بالنسبة للفقرة d قد تكون هي الأسهل لكن لم أعرف أين أضع الميثود.

هل الكود صحيح ام هناك أخطاء فيه أرجو الإطلاع والمساعدة.

كل الشكر

#2

السلام عليكــم ورحمـة الله وبركاتــه

أول ملاحظة، في Java، المتفق عليه هو أن إسم الclass يبتدأ بcapital letter، والمتحولات تبتدأ بminuscule

الملاحظة الثانية : في السؤال

اقتباس
A no-argument constructor which initializes a part's ID to "0000", its description to "No part information", and its quantity to 0.

مطلوب no-argument constructor، وهو لا يوجد في الكود

الملاحظة التالثة : أظن أنه من المفروض أن تكون المتحولة quantity من نوع int وليس String

بالنسبة للميثود display، تكتبها داخل الclassكما فعلت بالنسبة للميثود الأخرى

#3
herch كتب:

السلام عليكــم ورحمـة الله وبركاتــه

أول ملاحظة، في Java، المتفق عليه هو أن إسم الclass يبتدأ بcapital letter، والمتحولات تبتدأ بminuscule

الملاحظة الثانية : في السؤال

مطلوب no-argument constructor، وهو لا يوجد في الكود

الملاحظة التالثة : أظن أنه من المفروض أن تكون المتحولة quantity من نوع int وليس String

بالنسبة للميثود display، تكتبها داخل الclassكما فعلت بالنسبة للميثود الأخرى

مالمقصود بـ

no-argument constructor

وهل باقي الكود صحيح؟

#4

السلام عليكــم ورحمـة الله وبركاتــه

يعني constructor بدون arguments


{
ID="0000";
Description="No part information";
Quantity=0;

}
   public inventory() //no arguments = 0 arguments

بالنسبة لباقي الكود ، فقد كتبت ملاحظاتي

#5

الله يعطيك العافيه أخوي

بس بالنسبة للميثود display كيف يمكنني كتبتها لتنفيد المطلوب في السؤال؟

وشكراً

#6
public void display()
{
	//System.out.println("...") print a text to the screen
	System.out.println("ID: " + ID + ", Description: " + Description + ", Quantity: " + Quantity);
}
#7

الله يعطيك العافيه أخوي ويزيدك علم فوق علم.

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