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

Pleez About The Inheritance

بدأه hooom في 4 نوفمبر 2008 · 5 رد · 1,987 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

i have a question and i tried to answer it but i still have a few errors..can someone please give me my code back with the correction plz ? :(.... and i need it in less then a day...i know am being pushy am so sorry but i really need it

the Q is

Consider a superclass PurchaseItem which models customer?s purchases. This class has:

- two private instance variables name (String) and unitPrice (double).

- One constructor to initialize the instance variables.

- A default constructor to initialize name to ?no item?, and unitPrice to 0. use this()

- A method getPrice that returns the unitPrice.

- Accessor and mutator methods.

- A toString method to return the name of the item followed by @ symbol, then the unitPrice.

Consider two subclasses WeighedItem and CountedItem. WeighedItem has an additional instance variable weight (double) in Kg while CountedItem has an additional variable quantity (int) both private.

- Write an appropriate constructor for each of the classes making use of the constructor of the superclass in defining those of the subclasses.

- Override getPrice method that returns the price of the purchasedItem based on its unit price and weight (WeighedItem), or quantity (CountedItem). Make use of getPrice of the superclass

- Override also toString method for each class making use of the toString method of the superclass in defining those of the subclasses.

toString should return something that can be printed on the receipt.

For example

Banana @ 3.00 1.37Kg 4.11 SR (in case of WeighedItem class)

Pens @ 4.5 10 units 45 SR (in case of CountedItem class)

Write an application class where you construct objects from the two subclasses and print them on the screen.

and i wrote this program

public class PurchaseItem {
private String name;
private double unitprice;

public PurchaseItem (String name , double unitprice) {
this.name = name;
this.uniteprice = unitprice;
}
public PurchaseItem() {
name = "no item" :
uniteprice = o;
}
public double getunitprice() {
return uniteprice;
}
public String getname() {
return name;
}
public void setName(String newName){
this.name = newName;
}
public void setuniteprice(double newuniteprice){
this.uniteprice = newuniteprice;
}
public String toString(){
return "\nitem name:" + name + "@" + "\nuniteprice:" + uniteprice;
}

}
public class WeighedItem extends PurchaseItem {
private double kg;
public WeighedItem(String name , double unitprice , double kg) {
super (name , uniteprice);
this.kg = kg;
}
public double getunitprice() {
return uniteprice + "" + kg;
}
public String toString() {
return "\nitem name:" + name + "@" +"\item weight:" + kg + "\nuniteprice:" + uniteprice;
}




}
public class counteditem extends PurchaseItem {
private int quantity;
public counteditem(String name , double unitprice , int quantity) {
super (name , uniteprice);
this.quantity = quantity;
}
public double getunitprice() {
return uniteprice + "" + quantity;
}
public String toString() {
return "\nitem name:" + name + "@" +"\quantity:" + quantity + "\nuniteprice:" + uniteprice;
}
}

تم تعديل هذه المشاركة بواسطة ahmad123 في 5 نوفمبر 2008 في 00:33 — السبب: الرجاء ترتيب المشاركه مره اخرى

#2

حياك الله ،

لكن أكتب سؤال بشكل واضح ويفضل بالعربي وأختصره قدر الإمكان + أستخدم خاصيه التنسيق في الكود ...

بالتوفيق ،

http://informatic-ar.com منصة تعليمية عربية في علوم الحاسب والبرمجة

https://moalfat.com  للكتب الالكترونية والكورسات التعليمية

Everything we see now is just an engineering solution based on old science

#3

Sorry but my keyboar is not have Arabic letter

Quastion :

Consider a superclass PurchaseItem which models customer?s purchases. This Class has:

- two private instance variables name (String) and unitPrice (double).

- One Constructor to initialize the instance variables.

- A default Constructor to initialize name to ?no item?, and unitPrice to 0. use this()

- A Method getPrice that returns the unitPrice.

- Accessor and mutator methods.

- A toString Method to return the name of the item followed by @ symbol, then the unitPrice.

Consider two subclasses WeighedItem and CountedItem. WeighedItem has an additional instance variable weight (double) in Kg while CountedItem has an additional variable quantity (int) both private.

- Write an appropriate Constructor for each of the classes making use of the Constructor of the superclass in defining those of the subclasses.

- Override getPrice Method that returns the price of the purchasedItem based on its unit price and weight (WeighedItem), or quantity (CountedItem). Make use of getPrice of the superclass

- Override also toString Method for each Class making use of the toString Method of the superclass in defining those of the subclasses.

toString should return something that can be printed on the receipt.

For example

Banana @ 3.00 1.37Kg 4.11 SR (in case of WeighedItem class)

Pens @ 4.5 10 units 45 SR (in case of CountedItem class)

Write an application Class where you construct objects from the two subclasses and print them on the screen

تم تعديل هذه المشاركة بواسطة ahmad123 في 5 نوفمبر 2008 في 00:35

#4

طيب تفصيل عن مشكلتك

أين المشكلة بالضبط

ما نوعيتها

ما الذي تتوقعه كحل لها

أسهب في الحديث

تحياتي

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#5

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


private String name;
private double unitprice;
public PurchaseItem (String name , double unitprice) {
this.name = name;
this.unitprice = unitprice;
}
public PurchaseItem() {
name = "no item";
unitprice = 0;
}
public double getunitprice() {
return unitprice;
}
public String getname() {
return name;
}
public void setName(String newName){
this.name = newName;
}
public void setunitprice(double newunitprice){
this.unitprice = newunitprice;
}
public String toString(){
return "\nitem name:" + name + " @ " + unitprice;
}
}

class WeighedItem extends PurchaseItem {
private double kg;
public WeighedItem(String name , double unitprice , double kg) {
super (name , unitprice);
this.kg = kg;
}
public double getunitprice() {
return super.getunitprice();
}
public String toString() {
//Banana @ 3.00 1.37Kg 4.11 SR
return super.toString()+ " " + kg + "Kg "+ (getunitprice()*kg) + " SR";
}
}

class counteditem extends PurchaseItem {
private int quantity;
public counteditem(String name , double unitprice , int quantity) {
super (name , unitprice);
this.quantity = quantity;
}
public double getunitprice() {
return super.getunitprice();
}
public String toString() {
//Pens @ 4.5 10 units 45 SR
return super.toString()+ " " + super.getunitprice() + " " +quantity + " units" + (getunitprice()*quantity) + " SR";
}
}
public class PurchaseItem {

تم تصحيح بعض أسماء الأخطاء مثل

public class تستخدم فقط في اسم الفئة التي تحمل اسم الملف ولازم تكون كلها بأحرف إنجليزية صغيرة small letters

unitprice أحيانا تكتبها uniteprice

وحاضرين ولو كنا متأخرين

#6

ألف شكر للأخوان وجزاكم الله كل خير .. وشكر خاص للأخ حمد الذي نورني على أمور كثيرة فظهرت بهذه النتيجة


{
private String name;
public double unitPrice;
public PurchaseItem(String name, double unitPrice)
{
this.name = name;
this.unitPrice = unitPrice;
}

public double getPrice()
{
return unitPrice;
}

public String toString()
{
return name + " @ " + unitPrice;
}
}
class WeighedItem extends PurchaseItem
{
private double weight;
public WeighedItem(String name, double unitPrice, double weight)
{
super(name,unitPrice);
this.weight = weight;
}

public double getPrice()
{
return weight*unitPrice;
}

public String toString()
{
return super.toString() + " " + weight + " Kg " + getPrice() +" SR ";
}
}

class CountedItem extends PurchaseItem
{
private int quantity;
public CountedItem(String name, double unitPrice, int quantity)
{
super(name,unitPrice);
this.quantity = quantity;
}
public double getprice()
{
return quantity*unitPrice;
}
public String toString()
{
return super.toString() + " " + quantity + " units "+ getprice()+" SR";
}
}
class PurchaseItemTest
{
public static void main (String[]args)
{
PurchaseItem A = new PurchaseItem("Banana",4.12);
WeighedItem B = new WeighedItem("Banana",4.12,4.11);
PurchaseItem C = new PurchaseItem("pens",3.6);
CountedItem D = new CountedItem("pens",3.6,45);

System.out.println(B);
System.out.println(D);


}

}
class PurchaseItem

أكرر شكري لكم

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