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

Creat clock by Java

مغلق
بدأه Abodia في 28 سبتمبر 2010 · 17 رد · 982 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

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

كيف حالكم اخواني, انا طالب بجامعة أمريكية وتخصصي نظم ادارة المعلومات.

أخذت مادة الجافا وانا مبتدئ فيها والدكتور امرنا انو نسوي ساعة تطبع الساعات والدقايق . أنا صراحة محتار معاه ما ادري كيف ابدء. حاولت اني اعمله بس صعب علي. لوتكرمتو احد يساعدني فيه.

النتيجة النهائية يطبع

run:

The current time is: 12:00 pm

The current time is: 1200 hours

The current time is: 1:00 pm

The current time is: 1300 hours

The current time is: 1:47 pm

The current time is: 1347 hours

The current time is: 4:17 pm

The current time is: 1617 hours

The current time is: 12:32 am

The current time is: 0032 hours

BUILD SUCCESSFUL (total time: 0 seconds

)

الكود الي انا شغال عليه الآن

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package clockdriver;

*/

public class ClockDriver {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

Clock MyClock = new Clock(1200); // Instantiate a clock object and initialize to 12 hours, 0 minutes

MyClock.displayAMPM(); // display time in AM or PM format

MyClock.displayMilitary(); // display time in military (HHMM) format

System.out.println(); // print blank line

MyClock.setHours(13); // change hours attribute to 13

MyClock.displayAMPM();

MyClock.displayMilitary();

System.out.println();

MyClock.setMinutes(47); // change minutes attribute to 47

MyClock.displayAMPM();

MyClock.displayMilitary();

System.out.println();

MyClock.advance(150); // advance time by 150 minutes (2 hours, 30 minutes)

MyClock.displayAMPM();

MyClock.displayMilitary();

System.out.println();

MyClock.advance(495); // advance time by 495 minutes (8 hours, 15 minutes)

MyClock.displayAMPM();

MyClock.displayMilitary();

}

}

class Clock{

/***************************************************************

* Objects of this class represent time (in hours and minutes) *

* The time data is stored in 24 hour (HHMM military format) *

***************************************************************/

private int hours; // valid range of values 0 thru' 23

private int minutes; // valid range of values 0 thru' 59

public Clock (int h, int m)

{

/*************************************************************************

** This method (constructor) must initialize hours and minutes **

** using the value of the integer variable h (20 points) **

*************************************************************************/

if(0<= hours && hours<23 )

h=hours;

else hours=0;

if(0<= m && m<59 )

m= minutes;

else m=0;

}

public void setHours(int h){

/*************************************************************************

** This method must set hours to the value of **

** the integer variable h (5 points) *

*************************************************************************/

if(0<= h && h<59 )

h= minutes;

else h=0;

System.out.println("h"+h);

}

public void setMinutes(int m){

/*************************************************************************

** This method must set minutes to the value of **

** the integer variable m (5 points) **

*************************************************************************/

if(0<= m && m<59 )

m= minutes;

else m=0;

System.out.println("m"+m);

}

public void displayAMPM(){

/*************************************************************************

** This method must display hours and minutes using the **

** AM or PM time format i.e., hh:mm AM/PM (20 points) **

*************************************************************************/

}

}

public void displayMilitary(){

/*************************************************************************

** This method must display hours and minutes using the **

** military time format i.e., hhmm hours (10 points) **

*************************************************************************/

}

public void advance(int m){

/*************************************************************************

** This method must modify the values of hours and minutes **

** to reflect advancing time by m minutes (40 points) **

*************************************************************************/

System.out.println(m,h);

}

}

#2

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

يا اخي الكريم اودان اسألك هل اذا كان المقصود طباعة

The current time is: 12:00 pm

The current time is: 1200 hours

فهناك method دالة جاهزة خاصة بالوقت والتاريخ

اما اذا كان الدكتور يريدها بطريقة يدوية normal فانشأ الله

في اليلل اعطيك حلها لان انا خارج البيت

#3
مجتمعي كتب:

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

يا اخي الكريم اودان اسألك هل اذا كان المقصود طباعة

The current time is: 12:00 pm

The current time is: 1200 hours

فهناك method دالة جاهزة خاصة بالوقت والتاريخ

اما اذا كان الدكتور يريدها بطريقة يدوية normal فانشأ الله

في اليلل اعطيك حلها لان انا خارج البيت

والله اكون لك شاكر اذا تساعدني في الحلين الأكواد الجاهز والطريقة اليدوية . الدكتور كاتب على كل كلاس توضيح بس برضو احس اني ضايع ما ادري كيف ابدء.

ربي يوفقك لما يحبه ويرضاه.

#4
Abodia كتب:

والله اكون لك شاكر اذا تساعدني في الحلين الأكواد الجاهز والطريقة اليدوية . الدكتور كاتب على كل كلاس توضيح بس برضو احس اني ضايع ما ادري كيف ابدء.

ربي يوفقك لما يحبه ويرضاه.

البرنامج يطبع بالنهاية

النتيجة هذه. هذا طلب الدكتور

The current time is: 12:00 pm

The current time is: 1200 hours

The current time is: 1:00 pm

The current time is: 1300 hours

The current time is: 1:47 pm

The current time is: 1347 hours

The current time is: 4:17 pm

The current time is: 1617 hours

The current time is: 12:32 am

The current time is: 0032 hours

BUILD SUCCESSFUL (total time: 0 seconds

#5
Abodia كتب:

البرنامج يطبع بالنهاية

النتيجة هذه. هذا طلب الدكتور

The current time is: 12:00 pm

The current time is: 1200 hours

The current time is: 1:00 pm

The current time is: 1300 hours

The current time is: 1:47 pm

The current time is: 1347 hours

The current time is: 4:17 pm

The current time is: 1617 hours

The current time is: 12:32 am

The current time is: 0032 hours

BUILD SUCCESSFUL (total time: 0 seconds

هذا آخر تعديلاتي على الكود

برضوى عندي مشكلة مع الطباعة ,يطبع وقت واحد مو عدة ارقام. أي مساعده

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package clockdriver;

/**

*

* @author Abdullah Albahrani2

*/

public class ClockDriver {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

Clock MyClock = new Clock(1200); // Instantiate a clock object and initialize to 12 hours, 0 minutes

MyClock.displayAMPM(); // display time in AM or PM format

MyClock.displayMilitary(); // display time in military (HHMM) format

System.out.println(); // print blank line

MyClock.setHours(13); // change hours attribute to 13

MyClock.displayAMPM();

MyClock.displayMilitary();

System.out.println();

MyClock.setMinutes(47); // change minutes attribute to 47

MyClock.displayAMPM();

MyClock.displayMilitary();

System.out.println();

MyClock.advance(150); // advance time by 150 minutes (2 hours, 30 minutes)

MyClock.displayAMPM();

MyClock.displayMilitary();

System.out.println();

MyClock.advance(495); // advance time by 495 minutes (8 hours, 15 minutes)

MyClock.displayAMPM();

MyClock.displayMilitary();

}

}

class Clock{

/***************************************************************

* Objects of this class represent time (in hours and minutes) *

* The time data is stored in 24 hour (HHMM military format) *

***************************************************************/

private int hours; // valid range of values 0 thru' 23

private int minutes; // valid range of values 0 thru' 59

public Clock () {

}

public Clock(int h){

/*************************************************************************

** This method (constructor) must initialize hours and minutes **

** using the value of the integer variable h (20 points) **

*************************************************************************/

Myclock (hours, minutes,);

}

}

public void setHours(int h){

/*************************************************************************

** This method must set hours to the value of **

** the integer variable h (5 points) **

*************************************************************************/

if (0 <= hours && hours < 24)

h = hours++;

else

h = hours;

System.out.println(h);

}

public void setMinutes(int m){

/*************************************************************************

** This method must set minutes to the value of **

** the integer variable m (5 points) **

*************************************************************************/

if (0 <= minutes && minutes < 60)

m = minutes++

else

m = minutes;

}

public void displayAMPM(){

/*************************************************************************

** This method must display hours and minutes using the **

** AM or PM time format i.e., hh:mm AM/PM (20 points) **

*************************************************************************/

MyClock.displayAMPM()

}

System.out.printf("Carrrently : ", MyClock.displayAMPM();

public void displayMilitary(){

/*************************************************************************

** This method must display hours and minutes using the **

** military time format i.e., hhmm hours (10 points) **

*************************************************************************/

MyClock.displayMilitary();

System.out.printf("Carrrently : ", MyClock.displayMilitary();

}

public void advance(int m){

/*************************************************************************

** This method must modify the values of hours and minutes **

** to reflect advancing time by m minutes (40 points) **

*************************************************************************/

System.out.printf("Carrrently : "+MyClock.advance(150*120);

System.out.printf("Carrrently : " +MyClock.advance(495);

}

#6
Abodia كتب:

هذا آخر تعديلاتي على الكود

برضوى عندي مشكلة مع الطباعة ,يطبع وقت واحد مو عدة ارقام. أي مساعده

اخى الكريم

محدش هيعرف يساعدك فالكود لان الكود غير منظم

وكمان الكود اساسا مكتوب غلط وبشكل غير مفهوم

هل انت متاكد ان هذا الكود الذى وضعته سليم

يعنى شغال ؟؟

الكود مش شغال خالص لانه فيه اخطاء كتير

بص الصوره دى كدا

post-217802-059851600 1285852413_thumb.p

المرفقات
errors.png

Software Developer
Mahmoudkelany.com


 

#7
Mahmoud Kelany كتب:

اخى الكريم

محدش هيعرف يساعدك فالكود لان الكود غير منظم

وكمان الكود اساسا مكتوب غلط وبشكل غير مفهوم

هل انت متاكد ان هذا الكود الذى وضعته سليم

يعنى شغال ؟؟

الكود مش شغال خالص لانه فيه اخطاء كتير

بص الصوره دى كدا

]

,والله عارف انو لخبطه أنا مبتدئ بالجافا هذا الذي استطيع أعملة . الدكتو ر كتب الكود وقال انتو بتضيفو عليه وتضبطوه , الأخطاء الي انت تشوفه ناتجه عن محاولاتي لكن مو عارف كيف ابدء, انا جديد على الجافا بحاول اتعلمها وأخذت كلاس مع هذا الدتكتور بالجامعه بس اول واجب مصعبه ClockDriver . أنا رايح اضع لك الكود من اساسه بدون اي اضافات , ارجو من سماحتكم المساعده فيه .

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

هذا التعليمات

Attached is a source code file that contains two classes. The

ClockDriver

class contains

main()

, and it is used to instantiate and operate methods of the

Clock

class. Do not change any code in the

main()

method. Your task is to implement the methods of the

Clock

class. The

Cloc

k object is meant to represent a 24 hour clock, and therefore should function as a conventional clock that stores and displays valid time values in the range of 0 - 23 hours and 0 - 59 minutes. Descriptions of the functionality of each of the methods is provided in the source code file. If correctly implemented, the output should look as follows:

compile:

run:

The current time is: 12:00 pm

The current time is: 1200 hours

The current time is: 1:00 pm

The current time is: 1300 hours

The current time is: 1:47 pm

The current time is: 1347 hours

The current time is: 4:17 pm

The current time is: 1617 hours

The current time is: 12:32 am

The current time is: 0032 hours

BUILD SUCCESSFUL (total time: 0 seconds)

الكود:

public class ClockDriver {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

Clock MyClock = new Clock(1200); // Instantiate a clock object and initialize to 12 hours, 0 minutes

MyClock.displayAMPM(); // display time in AM or PM format

MyClock.displayMilitary(); // display time in military (HHMM) format

System.out.println(); // print blank line

MyClock.setHours(13); // change hours attribute to 13

MyClock.displayAMPM();

MyClock.displayMilitary();

System.out.println();

MyClock.setMinutes(47); // change minutes attribute to 47

MyClock.displayAMPM();

MyClock.displayMilitary();

System.out.println();

MyClock.advance(150); // advance time by 150 minutes (2 hours, 30 minutes)

MyClock.displayAMPM();

MyClock.displayMilitary();

System.out.println();

MyClock.advance(495); // advance time by 495 minutes (8 hours, 15 minutes)

MyClock.displayAMPM();

MyClock.displayMilitary();

}

}

class Clock{

/***************************************************************

* Objects of this class represent time (in hours and minutes) *

* The time data is stored in 24 hour (HHMM military format) *

***************************************************************/

private int hours; // valid range of values 0 thru' 23

private int minutes; // valid range of values 0 thru' 59

public Clock(int h){

/*************************************************************************

** This method (constructor) must initialize hours and minutes **

** using the value of the integer variable h (20 points) **

*************************************************************************/

}

public void setHours(int h){

/*************************************************************************

** This method must set hours to the value of **

** the integer variable h (5 points) **

*************************************************************************/

}

public void setMinutes(int m){

/*************************************************************************

** This method must set minutes to the value of **

** the integer variable m (5 points) **

*************************************************************************/

}

public void displayAMPM(){

/*************************************************************************

** This method must display hours and minutes using the **

** AM or PM time format i.e., hh:mm AM/PM (20 points) **

*************************************************************************/

}

public void displayMilitary(){

/*************************************************************************

** This method must display hours and minutes using the **

** military time format i.e., hhmm hours (10 points) **

*************************************************************************/

}

public void advance(int m){

/*************************************************************************

** This method must modify the values of hours and minutes **

** to reflect advancing time by m minutes (40 points) **

*************************************************************************/

}

}

تم تعديل هذه المشاركة بواسطة Abodia في 30 سبتمبر 2010 في 18:21

#8

جرب يا سيدى

/**
 *
 * @author Mahmoud Kelany
 */
public class Clock {

    /***************************************************************
     * Objects of this class represent time (in hours and minutes) *
     * The time data is stored in 24 hour (HHMM military format) *
     ***************************************************************/
    private int hours; // valid range of values 0 thru' 23
    private int minutes; // valid range of values 0 thru' 59

    /*************************************************************************
     ** This method (constructor) must initialize hours and minutes **
     ** using the value of the integer variable h (20 points) **
     *************************************************************************/
    public Clock(int h) {
        int hs = h / 100;
        int ms = h - hs * 100;
        setHours(hs);
        setMinutes(ms);
    }

    /*************************************************************************
     ** This method must set hours to the value of **
     ** the integer variable h (5 points) **
     *************************************************************************/
    public void setHours(int h) {
        if (h >= 0 && h <= 23) {
            hours = h;
        } else {
            hours = 0;
        }
    }

    /*************************************************************************
     ** This method must set minutes to the value of **
     ** the integer variable m (5 points) **
     *************************************************************************/
    public void setMinutes(int m) {
        if (m >= 0 && m <= 59) {
            minutes = m;
        } else {
            minutes = 0;
        }
    }

    /*************************************************************************
     ** This method must display hours and minutes using the **
     ** AM or PM time format i.e., hh:mm AM/PM (20 points) **
     *************************************************************************/
    public void displayAMPM() {
        if (hours >= 13 && hours <= 23) {
            System.out.println("The current: " + (hours - 12) + ":" + minutes + " PM");
        } else if (hours == 0) {
            System.out.println("The current: " + hours + ":" + minutes + " PM");
        } else {
            System.out.println("The current: " + hours + ":" + minutes + " AM");
        }
    }

    /*************************************************************************
     ** This method must display hours and minutes using the **
     ** military time format i.e., hhmm hours (10 points) **
     *************************************************************************/
    public void displayMilitary() {
        System.out.println("The current: " + hours + "" + minutes + " Hours");
    }

    /*************************************************************************
     ** This method must modify the values of hours and minutes **
     ** to reflect advancing time by m minutes (40 points) **
     *************************************************************************/
    public void advance(int m) {
        int hs = m / 60;
        int ms = m - (hs * 60);
        setHours(hs);
        setMinutes(ms);
    }
}

/**
 * 
 * @author Mahmoud Kelany
 */
public class ClockDriver {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Clock MyClock = new Clock(1247); // Instantiate a clock object and initialize to 12 hours, 0 minutes
        MyClock.displayAMPM(); // display time in AM or PM format
        MyClock.displayMilitary(); // display time in military (HHMM) format
        System.out.println(); // print blank line

        MyClock.setHours(13); // change hours attribute to 13
        MyClock.displayAMPM();
        MyClock.displayMilitary();
        System.out.println();

        MyClock.setMinutes(47); // change minutes attribute to 47
        MyClock.displayAMPM();
        MyClock.displayMilitary();
        System.out.println();

        MyClock.advance(150); // advance time by 150 minutes (2 hours, 30 minutes)
        MyClock.displayAMPM();
        MyClock.displayMilitary();
        System.out.println();

        MyClock.advance(495); // advance time by 495 minutes (8 hours, 15 minutes)
        MyClock.displayAMPM();
        MyClock.displayMilitary();
    }
}

Software Developer
Mahmoudkelany.com


 

#9

:)

Mahmoud Kelany كتب:

جرب يا سيدى

/**
 *
 * @author Mahmoud Kelany
 */
public class Clock {

    /***************************************************************
     * Objects of this class represent time (in hours and minutes) *
     * The time data is stored in 24 hour (HHMM military format) *
     ***************************************************************/
    private int hours; // valid range of values 0 thru' 23
    private int minutes; // valid range of values 0 thru' 59

    /*************************************************************************
     ** This method (constructor) must initialize hours and minutes **
     ** using the value of the integer variable h (20 points) **
     *************************************************************************/
    public Clock(int h) {
        int hs = h / 100;
        int ms = h - hs * 100;
        setHours(hs);
        setMinutes(ms);
    }

    /*************************************************************************
     ** This method must set hours to the value of **
     ** the integer variable h (5 points) **
     *************************************************************************/
    public void setHours(int h) {
        if (h >= 0 && h <= 23) {
            hours = h;
        } else {
            hours = 0;
        }
    }

    /*************************************************************************
     ** This method must set minutes to the value of **
     ** the integer variable m (5 points) **
     *************************************************************************/
    public void setMinutes(int m) {
        if (m >= 0 && m <= 59) {
            minutes = m;
        } else {
            minutes = 0;
        }
    }

    /*************************************************************************
     ** This method must display hours and minutes using the **
     ** AM or PM time format i.e., hh:mm AM/PM (20 points) **
     *************************************************************************/
    public void displayAMPM() {
        if (hours >= 13 && hours <= 23) {
            System.out.println("The current: " + (hours - 12) + ":" + minutes + " PM");
        } else if (hours == 0) {
            System.out.println("The current: " + hours + ":" + minutes + " PM");
        } else {
            System.out.println("The current: " + hours + ":" + minutes + " AM");
        }
    }

    /*************************************************************************
     ** This method must display hours and minutes using the **
     ** military time format i.e., hhmm hours (10 points) **
     *************************************************************************/
    public void displayMilitary() {
        System.out.println("The current: " + hours + "" + minutes + " Hours");
    }

    /*************************************************************************
     ** This method must modify the values of hours and minutes **
     ** to reflect advancing time by m minutes (40 points) **
     *************************************************************************/
    public void advance(int m) {
        int hs = m / 60;
        int ms = m - (hs * 60);
        setHours(hs);
        setMinutes(ms);
    }
}

/**
 * 
 * @author Mahmoud Kelany
 */
public class ClockDriver {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Clock MyClock = new Clock(1247); // Instantiate a clock object and initialize to 12 hours, 0 minutes
        MyClock.displayAMPM(); // display time in AM or PM format
        MyClock.displayMilitary(); // display time in military (HHMM) format
        System.out.println(); // print blank line

        MyClock.setHours(13); // change hours attribute to 13
        MyClock.displayAMPM();
        MyClock.displayMilitary();
        System.out.println();

        MyClock.setMinutes(47); // change minutes attribute to 47
        MyClock.displayAMPM();
        MyClock.displayMilitary();
        System.out.println();

        MyClock.advance(150); // advance time by 150 minutes (2 hours, 30 minutes)
        MyClock.displayAMPM();
        MyClock.displayMilitary();
        System.out.println();

        MyClock.advance(495); // advance time by 495 minutes (8 hours, 15 minutes)
        MyClock.displayAMPM();
        MyClock.displayMilitary();
    }
}

الله يعطيك العافيه. إن شاء الله في ميزان حسناتك . بجربه وبرد عليك إن شاء الله

:)

#10
Abodia كتب:

:)

الله يعطيك العافيه. إن شاء الله في ميزان حسناتك . بجربه وبرد عليك إن شاء الله

:)

الكود شغال تسلم يغالي الله يوفقك اخي وننتظر منك اكثر . اذا ما عليك امر توضح لي العمليه الحسابية

public Clock(int h) {

int hs = h / 100;

int ms = h - hs * 100;

setHours(hs);

setMinutes(ms);

هنا انت قسمت عدد السعات 1200 على 100 وبعدين ضربت عدد الساعات في 100 بعدين طرحت من ms= h

ما استوعبت هذا الكود .

الكود الثاني

public void advance(int m) {

int hs = m / 60;

int ms = m - (hs * 60);

setHours(hs);

setMinutes(ms);

hs عرفتها كعدد الساعات صح , هلm =0 وكل مرة يتغير العدد يعني دقيقة نقسمها على 60 وبعدين دقيقتين...... . ممكن الله لايهينك توضح لي الكود هذا.

الثالث

if (hours >= 13 && hours <= 23) {

System.out.println("The current: " + (hours - 12) + ":" + minutes + " PM");

} else if (hours == 0) {

System.out.println("The current: " + hours + ":" + minutes + " PM");

} else {

System.out.println("The current: " + hours + ":" + minutes + " AM");

}

}

ألاحظ انك بديت if بساعة اكبر من 13 ليه ما اقدر اغير الرقم الى 12 انا عملتها طبع لي 0 من الرغم انو لوطرحنا 13 ساعة من 12 يعطي ساعة 1 .

تم تعديل هذه المشاركة بواسطة Abodia في 1 أكتوبر 2010 في 08:21

#11
  public Clock(int h) {
        int hs = h / 100;
        int ms = h - hs * 100;
        setHours(hs);
        setMinutes(ms);
    }

انا قسمت على 100 علشان اجيب اخر رقمين اللى هى تعبر عن الساعه

وبعدين ضربت الساعه اللى طلعت فى 100 تانى علشان اطرحها من الرقم الاصلى علشان اجيب الدقائق

public void advance(int m) {
        int hs = m / 60;
        int ms = m - (hs * 60);
        setHours(hs);
        setMinutes(ms);
    }

انا قسمت على 60 علشان اجيب عدد الساعات

وبعدين ضربت عدد الساعات فى 60 تانى وطرحتها من الرقم الاصلى علشان اجيب الدقايق

نفس الفكره اللى فوق

   public void displayAMPM() {
        if (hours >= 13 && hours <= 23) {
            System.out.println("The current: 0" + (hours - 12) + ":" + minutes + " PM");
        } else if (hours == 0) {
            System.out.println("The current: 0" + hours + ":" + minutes + " PM");
        } else {
            System.out.println("The current: " + hours + ":" + minutes + " AM");
        }
    }

استخدمت الـ if علشان احدد الساعه دى AM ولا PM

انا بدات ب13 لان الـ 13 دى هى اول ساعه فى الـ PM

وعلشان اطلع الساعه بالنظام صباحى ومسائى

لو كانت الساعه من 13 الى 23 طرحت من 12 علشان يظهر لى الرقم وبعدها اضيف له PM

على فكرا لو كنت ركزت فى الكود كويس

ومشيت بالعمليات الحسابيه يدويا

لكنت فهمت معنى الاكواد

سلام عليكم

Software Developer
Mahmoudkelany.com


 

#12
Mahmoud Kelany كتب:

  public Clock(int h) {
        int hs = h / 100;
        int ms = h - hs * 100;
        setHours(hs);
        setMinutes(ms);
    }

انا قسمت على 100 علشان اجيب اخر رقمين اللى هى تعبر عن الساعه

وبعدين ضربت الساعه اللى طلعت فى 100 تانى علشان اطرحها من الرقم الاصلى علشان اجيب الدقائق

public void advance(int m) {
        int hs = m / 60;
        int ms = m - (hs * 60);
        setHours(hs);
        setMinutes(ms);
    }

انا قسمت على 60 علشان اجيب عدد الساعات

وبعدين ضربت عدد الساعات فى 60 تانى وطرحتها من الرقم الاصلى علشان اجيب الدقايق

نفس الفكره اللى فوق

   public void displayAMPM() {
        if (hours >= 13 && hours <= 23) {
            System.out.println("The current: 0" + (hours - 12) + ":" + minutes + " PM");
        } else if (hours == 0) {
            System.out.println("The current: 0" + hours + ":" + minutes + " PM");
        } else {
            System.out.println("The current: " + hours + ":" + minutes + " AM");
        }
    }

استخدمت الـ if علشان احدد الساعه دى AM ولا PM

انا بدات ب13 لان الـ 13 دى هى اول ساعه فى الـ PM

وعلشان اطلع الساعه بالنظام صباحى ومسائى

لو كانت الساعه من 13 الى 23 طرحت من 12 علشان يظهر لى الرقم وبعدها اضيف له PM

على فكرا لو كنت ركزت فى الكود كويس

ومشيت بالعمليات الحسابيه يدويا

لكنت فهمت معنى الاكواد

سلام عليكم

:) مشكور يغالي . والله انك عطيتني دافع اني اواصل دراسة الجافا حتى الإحتراف

#13
Abodia كتب:

:) مشكور يغالي . والله انك عطيتني دافع اني اواصل دراسة الجافا حتى الإحتراف

العفو اخى الكريم

ربنا يوفققك ان شاء الله

وان شاء الله ربنا يحقق لك اللى فى بالك

بالتوفيق :)

Software Developer
Mahmoudkelany.com


 

#14

انا غيرت لك الشكل بتاع الطباعه لانى كنت مستعجل لم كنت بعمل البرنامج

بحيث ان شكل الطباعه يقبى زى الشكل بتاع الاجابه بالظبط ان شاء الله

غيرت فالدالتين المسئولين عن الطباعه

public void displayAMPM() {
        if (hours >= 13 && hours <= 23) {
            System.out.format("The current: %02d:%02d PM%n", (hours - 12), minutes);
        } else if (hours == 0) {
            System.out.format("The current: %02d:%02d PM%n", hours, minutes);
        } else {
            System.out.printf("The current: %02d:%02d AM%n", hours, minutes);
        }
    }


  public void displayMilitary() {
        System.out.format("The current: %02d%02d Hours%n", hours, minutes);
    }

لو عاوز تفهم معنى ايه الحروف والرموز زى مثلا %02d

انظر الى هذا الموقع

http://download.oracle.com/javase/tutorial/java/data/numberformat.html

بالتوفيق

سلام عليكم

Software Developer
Mahmoudkelany.com


 

#15
Mahmoud Kelany كتب:

انا غيرت لك الشكل بتاع الطباعه لانى كنت مستعجل لم كنت بعمل البرنامج

بحيث ان شكل الطباعه يقبى زى الشكل بتاع الاجابه بالظبط ان شاء الله

غيرت فالدالتين المسئولين عن الطباعه

public void displayAMPM() {
        if (hours >= 13 && hours <= 23) {
            System.out.format("The current: %02d:%02d PM%n", (hours - 12), minutes);
        } else if (hours == 0) {
            System.out.format("The current: %02d:%02d PM%n", hours, minutes);
        } else {
            System.out.printf("The current: %02d:%02d AM%n", hours, minutes);
        }
    }




  public void displayMilitary() {
        System.out.format("The current: %02d%02d Hours%n", hours, minutes);
    }

لو عاوز تفهم معنى ايه الحروف والرموز زى مثلا %02d

انظر الى هذا الموقع

http://download.oracle.com/javase/tutorial/java/data/numberformat.html

بالتوفيق

سلام عليكم

تسلم اخي بس عندي مشكلة وحده النتيجة فيها اختلاف شوي بالوقت والكود الأول اسهل من الثاني الي ضفته مؤخرا انا حاولت اضيف عليه بعض التعديلات. بس المشكلة بالعدد الأخير ماضبط الوقت معاي. الدكتور قالي المشكلة في Advance المعادلة الأخيره انا غيرت فيها وصلت لعدد قريب جدا لنهائي بس مو بالضبط .

الكود

int hours1 = m / 60;

int minutes1 = m- (hours1 * 60);

setHours(hours1+hours+++1-hours1+++2);

setMinutes(minutes1---13);

int hours1 = m / 60;

int minutes1 = m- (hours1 * 60);

setHours(hours1+hours+++1-hours1+++2);

setMinutes(minutes1---13);

أنا وصلة الى العدد قبل الأخير صح بس الأخير ما اعرف كيف اجيبه ماضبط معاي

run:

The current: 12:0 AM

The current: 120 Hours

The current: 1:0 PM

The current: 130 Hours

The current: 1:47 PM

The current: 1347 Hours

The current: 4:17 PM

The current: 1617 Hours

The current: 7:2 PM

The current: 192 Hours

BUILD SUCCESSFUL (total time: 1 second)

هذا المخرجات المطلوبة

The current time is: 12:00 pm

The current time is: 1200 hours

The current time is: 1:00 pm

The current time is: 1300 hours

The current time is: 1:47 pm

The current time is: 1347 hours

The current time is: 4:17 pm

The current time is: 1617 hours

The current time is: 12:32 am

The current time is: 0032 hours

الله يعطيك العافية على مجهودك

بعد 3 الساعات من الآن الوقت النهائي لتسليم للبرنامج اذا امكنك اتشوف المشكلة وين الله يجزاك خير.

#16

لم الاحظ ان الدكتور يريد ان يضيف الـ 150 دقيقه على الوقت الموجود حالا

انا افتكرت انه يريد ان يحسب الوقت بناء على ال150 دقيقه وهكذا

المهم

هذا هو الكود الاخير

/**
 *
 * @author Mahmoud Kelany
 */
public class Clock {

    /***************************************************************
     * Objects of this class represent time (in hours and minutes) *
     * The time data is stored in 24 hour (HHMM military format) *
     ***************************************************************/
    private int hours; // valid range of values 0 thru' 23
    private int minutes; // valid range of values 0 thru' 59

    /*************************************************************************
     ** This method (constructor) must initialize hours and minutes **
     ** using the value of the integer variable h (20 points) **
     *************************************************************************/
    public Clock(int h) {
        int hs = h / 100;
        int ms = h - hs * 100;
        setHours(hs);
        setMinutes(ms);
    }

    /*************************************************************************
     ** This method must set hours to the value of **
     ** the integer variable h (5 points) **
     *************************************************************************/
    public void setHours(int h) {
        if (h >= 0 && h <= 23) {
            hours = h;
        } else {
            hours = 0;
        }
    }

    /*************************************************************************
     ** This method must set minutes to the value of **
     ** the integer variable m (5 points) **
     *************************************************************************/
    public void setMinutes(int m) {
        if (m >= 0 && m <= 59) {
            minutes = m;
        } else {
            minutes = 0;
        }
    }

    /*************************************************************************
     ** This method must display hours and minutes using the **
     ** AM or PM time format i.e., hh:mm AM/PM (20 points) **
     *************************************************************************/
    public void displayAMPM() {
        if (hours >= 13 && hours <= 23) {
            System.out.format("The current: %02d:%02d PM%n", (hours - 12), minutes);
        } else if (hours == 24) {
            hours -= 24;
            System.out.format("The current: %02d:%02d PM%n", hours, minutes);
            hours += 24;
        } else {
            System.out.printf("The current: %02d:%02d AM%n", hours, minutes);
        }
    }

    /*************************************************************************
     ** This method must display hours and minutes using the **
     ** military time format i.e., hhmm hours (10 points) **
     *************************************************************************/
    public void displayMilitary() {
        if (hours == 24) {
            hours -= 24;
            System.out.format("The current: %02d%02d Hours%n", hours, minutes);
            hours += 24;
        } else {
            System.out.format("The current: %02d%02d Hours%n", hours, minutes);
        }
    }

    /*************************************************************************
     ** This method must modify the values of hours and minutes **
     ** to reflect advancing time by m minutes (40 points) **
     *************************************************************************/
    public void advance(int m) {
        int hs = m / 60;
        int ms = m - (hs * 60);
        hours += hs;
        hours += (ms + minutes) / 60;
        minutes = (ms + minutes) % 60;
    }
}

/**
 * 
 * @author Mahmoud Kelany
 */
public class ClockDriver {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Clock MyClock = new Clock(1200); // Instantiate a clock object and initialize to 12 hours, 0 minutes
        MyClock.displayAMPM(); // display time in AM or PM format
        MyClock.displayMilitary(); // display time in military (HHMM) format
        System.out.println(); // print blank line

        MyClock.setHours(13); // change hours attribute to 13
        MyClock.displayAMPM();
        MyClock.displayMilitary();
        System.out.println();

        MyClock.setMinutes(47); // change minutes attribute to 47
        MyClock.displayAMPM();
        MyClock.displayMilitary();
        System.out.println();

        MyClock.advance(150); // advance time by 150 minutes (2 hours, 30 minutes)
        MyClock.displayAMPM();
        MyClock.displayMilitary();
        System.out.println();

        MyClock.advance(495); // advance time by 495 minutes (8 hours, 15 minutes)
        MyClock.displayAMPM();
        MyClock.displayMilitary();

    }
}

حظك انى صحيت دلوقتى اصل انت بتقول فاضل 3 ساعات بس :D

يلا خير بالتوفيق :)

Software Developer
Mahmoudkelany.com


 

#17
Mahmoud Kelany كتب:

لم الاحظ ان الدكتور يريد ان يضيف الـ 150 دقيقه على الوقت الموجود حالا

انا افتكرت انه يريد ان يحسب الوقت بناء على ال150 دقيقه وهكذا

المهم

هذا هو الكود الاخير

/**
 *
 * @author Mahmoud Kelany
 */
public class Clock {

    /***************************************************************
     * Objects of this class represent time (in hours and minutes) *
     * The time data is stored in 24 hour (HHMM military format) *
     ***************************************************************/
    private int hours; // valid range of values 0 thru' 23
    private int minutes; // valid range of values 0 thru' 59

    /*************************************************************************
     ** This method (constructor) must initialize hours and minutes **
     ** using the value of the integer variable h (20 points) **
     *************************************************************************/
    public Clock(int h) {
        int hs = h / 100;
        int ms = h - hs * 100;
        setHours(hs);
        setMinutes(ms);
    }

    /*************************************************************************
     ** This method must set hours to the value of **
     ** the integer variable h (5 points) **
     *************************************************************************/
    public void setHours(int h) {
        if (h >= 0 && h <= 23) {
            hours = h;
        } else {
            hours = 0;
        }
    }

    /*************************************************************************
     ** This method must set minutes to the value of **
     ** the integer variable m (5 points) **
     *************************************************************************/
    public void setMinutes(int m) {
        if (m >= 0 && m <= 59) {
            minutes = m;
        } else {
            minutes = 0;
        }
    }

    /*************************************************************************
     ** This method must display hours and minutes using the **
     ** AM or PM time format i.e., hh:mm AM/PM (20 points) **
     *************************************************************************/
    public void displayAMPM() {
        if (hours >= 13 && hours <= 23) {
            System.out.format("The current: %02d:%02d PM%n", (hours - 12), minutes);
        } else if (hours == 24) {
            hours -= 24;
            System.out.format("The current: %02d:%02d PM%n", hours, minutes);
            hours += 24;
        } else {
            System.out.printf("The current: %02d:%02d AM%n", hours, minutes);
        }
    }

    /*************************************************************************
     ** This method must display hours and minutes using the **
     ** military time format i.e., hhmm hours (10 points) **
     *************************************************************************/
    public void displayMilitary() {
        if (hours == 24) {
            hours -= 24;
            System.out.format("The current: %02d%02d Hours%n", hours, minutes);
            hours += 24;
        } else {
            System.out.format("The current: %02d%02d Hours%n", hours, minutes);
        }
    }

    /*************************************************************************
     ** This method must modify the values of hours and minutes **
     ** to reflect advancing time by m minutes (40 points) **
     *************************************************************************/
    public void advance(int m) {
        int hs = m / 60;
        int ms = m - (hs * 60);
        hours += hs;
        hours += (ms + minutes) / 60;
        minutes = (ms + minutes) % 60;
    }
}

/**
 * 
 * @author Mahmoud Kelany
 */
public class ClockDriver {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Clock MyClock = new Clock(1200); // Instantiate a clock object and initialize to 12 hours, 0 minutes
        MyClock.displayAMPM(); // display time in AM or PM format
        MyClock.displayMilitary(); // display time in military (HHMM) format
        System.out.println(); // print blank line

        MyClock.setHours(13); // change hours attribute to 13
        MyClock.displayAMPM();
        MyClock.displayMilitary();
        System.out.println();

        MyClock.setMinutes(47); // change minutes attribute to 47
        MyClock.displayAMPM();
        MyClock.displayMilitary();
        System.out.println();

        MyClock.advance(150); // advance time by 150 minutes (2 hours, 30 minutes)
        MyClock.displayAMPM();
        MyClock.displayMilitary();
        System.out.println();

        MyClock.advance(495); // advance time by 495 minutes (8 hours, 15 minutes)
        MyClock.displayAMPM();
        MyClock.displayMilitary();

    }
}

حظك انى صحيت دلوقتى اصل انت بتقول فاضل 3 ساعات بس :D

يلا خير بالتوفيق :)

مشكور حبيي على جهدك بس هذا الدكتور احس انو معقد المادة

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package clockdriverapp;

/**

*

* @author ashoksubramanian

*/

public class ClockDriver {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

Clock MyClock = new Clock(1200); // Instantiate a clock object

MyClock.displayAMPM(); // display time in AM or PM format

MyClock.displayMilitary(); // display time in military (HHMM) format

System.out.println(); // print blank line

MyClock.setHours(13); // change hours attribute to 13

MyClock.displayAMPM();

MyClock.displayMilitary();

System.out.println();

MyClock.setMinutes(47); // change minutes attribute to 47

MyClock.displayAMPM();

MyClock.displayMilitary();

System.out.println();

MyClock.advance(150); // advance time 150 minutes

MyClock.displayAMPM();

MyClock.displayMilitary();

System.out.println();

MyClock.advance(495); // advance time by 495 minutes

MyClock.displayAMPM();

MyClock.displayMilitary();

}

}

class Clock{

/***************************************************************

* Objects of this class represent time (in hours and minutes) *

* The time data is stored in 24 hour (HHMM military format) *

***************************************************************/

private int hours; // range of values 0 thru' 23

private int minutes; // range of values 0 thru' 59

public Clock(int h){

hours = h/100;

minutes = h%100;

}

public void setHours(int h){

hours = (h>23? h-24: h);

}

public void setMinutes(int m){

minutes= (m>59? m-60 : m);

}

public void displayAMPM(){

System.out.println("The current time is: " + (hours>12? hours-12 : hours<1? 12 : hours) + ":" + (minutes<10? ("0"+minutes) : minutes) + (hours>=12? " pm" : " am"));

}

public void displayMilitary(){

System.out.println("The current time is: " + (hours<10? "0"+hours: hours) + (minutes<10? "0"+minutes : minutes) + " hours");

}

public void advance(int m){

int totalminutes = (hours*60)+(minutes+m);

hours = (totalminutes/60)%24;

minutes=totalminutes%60;

/*******Alternative method***********

* minutes+=m;

* if(minutes>=60){

* hours+=(int)minutes/60;

* minutes%=60;

* }

*

* if (hours>=24) hours%=24;

*************************************/

}

}

هذا النهائي بس والله خسرة بعض النقاط ويقولي طريقتك خطأ

الأان نكمل على الكود نفسه بس بنضيف الأيام علىيه , يعني نبدء من يوم الخميس مثل كذا

Modify the Clock class from your previous assignment to include the following functionality:

* It should store, manipulate and display the day of the week

* It should be able to reverse the clock in addition to advancing it.

You are provided with a ClockDriver class to test the functionality of the Clock class. Do not change anything in the ClockDriver class. If your Clock class works correctly you should see the following output:

compile:

run:

Thursday, 12:00 pm

Thursday, 2:30 pm

Friday, 12:15 am

Friday, 12:15 am

Thursday, 2:30 pm

Saturday, 2:30 pm

---------------------------------------------------------------

هذا الي وصلت اليه بس الكود بيطبع رقم اليوم , وانا اريده يطبع اليوم غيرت في الرقم خمسة الى نص بس مايقبل .

الكود:

package clockdriver;

public class ClockDriver {

public static void main(String[] args) {

Clock MyClock = new Clock("Thursday", 12,0); // Instantiate a clock object

System.out.println(MyClock); // Should display: Thursday, 12:00 p.m.

System.out.println(); // print blank line

MyClock.advance(150); // advance time 150 minutes (2 hours 30 minutes)

System.out.println(MyClock); // Should display: Thursday, 2:30 p.m.

System.out.println();

MyClock.advance(585); // advance time by 585 minutes (9 hours 45 minutes)

System.out.println(MyClock); // Should display: Friday, 12:15 a.m.

System.out.println();

MyClock.advance(10080); // advance time by 10080 minutes (7 days)

System.out.println(MyClock); // Should display: Friday, 12:15 a.m.

System.out.println();

MyClock.reverse(585); // reverse time by 585 minutes (9 hours 45 minutes)

System.out.println(MyClock); // Should display: Thursday, 2:30 p.m.

System.out.println();

MyClock.reverse(17280); // reverse time by 17280 minutes (12 days)

System.out.println(MyClock); // Should display: Saturday, 2:30 p.m.

System.out.println();

} // end of main

} // end of class ClockDriver

class Clock{

/***************************************************************

* Objects of this class represent time (in hours and minutes) *

* The time data is stored in 24 hour (HHMM military format) *

***************************************************************/

private int day_of_week; // valid range of values is 1 thru' 7

private int hours; // range of values is 0 thru' 23

private int minutes; // range of values is 0 thru' 59

public Clock(String s, int h, int m){

day_of_week=5;

hours=0;

minutes=0;

}

public void advance(int m){

int totalminutes = (hours*60)+(minutes+m);

day_of_week=totalminutes%8;

hours = (totalminutes/60)%24;

minutes=totalminutes%60;

}

public void reverse(int m){

int totalminutes = (hours*60)-(minutes-m);

day_of_week=totalminutes%8;

hours = (totalminutes%24)/60;

minutes=totalminutes%60;

}

@Override

public String toString(){

String S = + day_of_week + ", " +

(hours>12? hours-12 : hours<1? 12 : hours) +

":" + (minutes<10? ("0"+minutes) : minutes) +

(hours>=12? " pm" : " am");

return (S);

}

}

النتيجة

run:

5, 12:00 am

6, 2:30 am

7, 12:15 pm

7, 12:15 pm

2, 12:30 am

2, 12:30 am

BUILD SUCCESSFUL (total time: 0 seconds)

----------------------------------------------

تسلم محمد على المساعدة الأخيرة وإن شاء الله في ميزان حسناتك

وعذرني على الرد متأخر لأني ما اعرف احك راسي وهذا الدكتور جاب بينا العيد .

تم تعديل هذه المشاركة بواسطة Abodia في 11 نوفمبر 2010 في 11:30

#18

اخى الكريم

يجب فتح موضوع جديد باسئلتك الجديده

وشكرا

Software Developer
Mahmoudkelany.com


 

هذا الموضوع مغلق.

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