صفحه 2 از 3 اولاول 123 آخرآخر
نمایش نتایج 41 تا 80 از 104

نام تاپیک: مجموعه برنامه‌ های نوشته شده به زبان جاوا

  1. #41

    نقل قول: مجموعه برنامه‌های نوشته شده به زبان جاوا

    تبدیل اعداد چهاررقمی به صورت حروفی
    *
    * in barname baray tabdil yek adad chahar raghami be horof ast.
    *
    */

    import java.util.Scanner;


    public class num_convert {


    public static void main(String[] args) {
    int a = 0,b = 0,c = 0 , counter=1;
    int n=0 , m=0;
    Scanner in = new Scanner(System.in);
    System.out.print( "Input A Number In Range Of 1000-9999?\n" ) ;
    n = in.nextInt();

    if ( n >= 10000 || n < 1000 )
    {
    System.out.print ( "Your Number Should Be In Range Of 1000 -9999\n " );
    counter=3 ;
    }
    m = n ;
    while ( counter <= 2 )
    {
    if ( counter == 1)

    n = n / 1000 ;

    else if ( counter == 2 )

    n = m % 1000 ;
    c = n % 10 ;
    n = n / 10 ;
    b = n % 10 ;
    n = n / 10 ;
    a = n % 10 ;
    n = n / 10 ;
    if ( a == 1 )
    System.out.print ( "sado " ) ;
    else if ( a == 2 )
    System.out.print ( "devisto " ) ;
    else if ( a == 3 )
    System.out.print ( "sisado " ) ;
    else if ( a == 4 )
    System.out.print ( "chaharsado " ) ;
    else if ( a == 5 )
    System.out.print ( "pansado " ) ;
    else if ( a == 6 )
    System.out.print ( "sheshsado " ) ;
    else if ( a == 7 )
    System.out.print ( "haftsado " ) ;
    else if ( a == 8 )
    System.out.print ( "hashtsado " ) ;
    else if ( a == 9 )
    System.out.print ( "nohsado " ) ;
    if ( b == 1 )
    {
    if ( c == 1 )
    System.out.print ( "yazdaho " ) ;
    else if ( c == 2 )
    System.out.print ( "davazdaho " ) ;
    else if ( c == 3 )
    System.out.print ( "sizdaho " ) ;
    else if ( c == 4 )
    System.out.print ( "chahardaho " ) ;
    else if ( c == 5 )
    System.out.print ( "panzdaho " ) ;
    else if ( c == 6 )
    System.out.print ( "shanzdaho " ) ;
    else if ( c == 7 )
    System.out.print ( "hefdaho " ) ;
    else if ( c == 8 )
    System.out.print ( "hejdaho " ) ;
    else if ( c == 9 )
    System.out.print ( "noozdaho " ) ;
    else if ( c == 0 )
    System.out.print ( "dah " ) ;
    }
    else if ( b == 2 )
    System.out.print ( "bisto " ) ;
    else if ( b == 3 )
    System.out.print ( "sio " ) ;
    else if ( b == 4 )
    System.out.print ( "chehelo " ) ;
    else if ( b == 5 )
    System.out.print ( "panjaho " ) ;
    else if ( b == 6 )
    System.out.print ( "shasto " ) ;
    else if ( b == 7 )
    System.out.print ( "haftado " ) ;
    else if ( b == 8 )
    System.out.print ( "hashtado " ) ;
    else if ( b == 9 )
    System.out.print ( "nohsado " ) ;
    if ( b != 1 )
    {
    if ( c == 1 )
    System.out.print ( "yek " ) ;
    if ( c == 2 )
    System.out.print ( "do " ) ;
    if ( c == 3 )
    System.out.print ( "se " ) ;
    if ( c == 4 )
    System.out.print ( "chahar " ) ;
    if ( c == 5 )
    System.out.print ( "panj " ) ;
    if ( c == 6 )
    System.out.print ( "shesh " ) ;
    if ( c == 7 )
    System.out.print ( "haft " ) ;
    if ( c == 8 )
    System.out.print ( "hasht " ) ;
    if ( c == 9 )
    System.out.print ( "noh " ) ;
    }
    if (counter == 1 && m >= 1000 )
    System.out.print ( "hezaro " ) ;
    counter = counter + 1 ;
    }

    }

    }

  2. #42

    نقل قول: مجموعه برنامه‌های نوشته شده به زبان جاوا

    حذف حروف تکراری از یک جمله وردی

    import java.io.*;


    public class removechar {



    public static void main(String[] args) throws Exception{
    {



    System.out.println("enter a statement:");

    BufferedReader br =new BufferedReader(new InputStreamReader(System.in));

    String inputString = br.readLine();

    System.out.println("Original String is: "+inputString);

    inputString = inputString.replaceAll((String.format("(.)(?<=(?:( ?=\\1).).{0,%d}(?:(?=\\1).))", inputString.length())), "");

    System.out.println("New String is: "+inputString);



    }

  3. #43
    کاربر تازه وارد آواتار m.a.d1992
    تاریخ عضویت
    دی 1391
    محل زندگی
    زاهدان
    پست
    71

    نقل قول: مجموعه برنامه‌های نوشته شده به زبان جاوا

    سلام
    منم در پاسخ به دوستان خوبی که برنامه های خودشونو میزارن و می خوان این تاپیک توسعه پیدا کنه گفتم چندتا برنامه که خودم نوشتم رو بزارم
    قبل از هر چیز یه کلاس میزارم که تقریبا همه جا استفاده میشه، البته توو پروژه های swing (یا خودمونی بگم همون ویندوزی )

    این کلاس دو تا متد داره که اولی پنجره یا فرمی که بش پاس میدی میزاره وسط مانیتور، فرقی نمیکنه مانیتورش چه اندازه ای باشه این متد دقیقا فرم شما رو میزاره وسط مانیتور!
    متد دوم کارش اینه که فرم رو از حالت خشک و یه نواختی که هست تبدیل به فرم ویندوزی میکنه یعنی کسی متوجه نمیشه که شما این برنامه رو با زبان جاوا نوشتین یا سی شارپ!


    من که از این کلاس توو همه ی برنامه های ویندوزی م استفاده کردم امیدوارم برای شما مفید باشه

    /*
    * class tools 2 ta tabe mohem ke estefade mishe dare
    *
    * Programer: AbdolsalamDehvari mad
    */
    package ToolsPackage;

    import java.awt.Toolkit;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.SwingUtilities;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;

    public class Tools {

    // in tabe baraye gharar dadane frame dar vasate monitor e.
    public static void setLocation(JFrame jframe) {
    double w = Toolkit.getDefaultToolkit().getScreenSize().getWid th();
    double h = Toolkit.getDefaultToolkit().getScreenSize().getHei ght();
    w = (w - jframe.getWidth()) / 2;
    h = (h - jframe.getHeight()) / 2;
    jframe.setLocation((int) w, (int) h);
    }

    // in tabe baraye windows y kardane frame ha estefade mishe.
    public static void setWindowsStyle(JFrame jf) {
    try {
    UIManager.setLookAndFeel("com.sun.java.swing.plaf. windows.WindowsLookAndFeel");
    SwingUtilities.updateComponentTreeUI(jf);
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
    JOptionPane.showMessageDialog(null, e, "WindowsStyle", 0);
    }
    }
    }

  4. #44
    کاربر تازه وارد آواتار m.a.d1992
    تاریخ عضویت
    دی 1391
    محل زندگی
    زاهدان
    پست
    71

    نقل قول: مجموعه برنامه‌های نوشته شده به زبان جاوا

    نمی دونم مسئله برج هانوی رو شنیدین یا نه، اگه نشنیدین کافی یه سرچ بزنین خیلی مسئله ی جالبه، برنامه زیر این مسئله رو حل میکنه


    package borje_hanoy;
    import java.util.Scanner;

    public class Borje_hanoy
    {
    public static void hanoy (int N, char a, char b, char c)
    {
    if( N == 1 )
    {
    System.out.println("Disk 1 from " + a + " to " + c );
    }
    else
    {
    hanoy ( N-1, a, c, b);
    System.out.println( "Disk " + N + " from "+ a + " to " + c);
    hanoy( N-1, b, a, c );
    }
    }
    public static void main(String[] args)
    {
    Scanner s = new Scanner(System.in);
    int disk;
    System.out.println("tedade disk ra vared konid");
    disk = s.nextInt();
    Borje_hanoy.hanoy( disk, 'A', 'B', 'C' );
    }
    }

  5. #45
    کاربر تازه وارد آواتار m.a.d1992
    تاریخ عضویت
    دی 1391
    محل زندگی
    زاهدان
    پست
    71

    نقل قول: مجموعه برنامه‌های نوشته شده به زبان جاوا

    برنامه ی زیر رو برای یکی از دوستام نوشتم، امید وارم براتوون جالب باشه

    کارش اینه یه سری اطلاعات مثل نام، نام خانوادگی، .... شماره بیمه و نوع بیمه رو میگیره و میریزه توو یه آرایه بعد یه نام میگره اگه نام توو آرایه باشه اطلاعات بیمه ش رو چاپ میکنه و اگه وجود نداتشته باشه که هیچی . .

    /*
    * dty vadte blch
    */
    package bimeh;

    import java.util.Scanner;


    public class Bimeh {
    /**
    * @param args the command line arguments
    */
    static public void Search(String[][] A, String Nam, int tedad) {
    for (int i = 0; i < tedad; i++) {
    if (A[i][0].equals(Nam)) {
    System.out.println("nam: " + A[i][0]
    + " famil: " + A[i][1]
    + " nam pedar: " + A[i][2]
    + " shomare shenasname: " + A[i][3]
    + " shomare bime: " + A[i][4]
    + " no'e bime: " + A[i][5]);
    } // end if
    } // end for
    } // end Search method
    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String[][] bimeshodeghan = new String[1000][6];
    int tedad; // tedade bime shodeghan vared shode
    boolean kontrol = true;

    System.out.println("tedad ra vared konid:");
    tedad = in.nextInt();

    for (int i = 0; i < tedad; i++) {
    System.out.println("nam ra vared konid:");
    bimeshodeghan[i][0] = in.next();
    System.out.println("famil ra vared konid:");
    bimeshodeghan[i][1] = in.next();
    System.out.println("nam pedar ra vared konid:");
    bimeshodeghan[i][2] = in.next();
    System.out.println("shomare shenasname ra vared konid:");
    bimeshodeghan[i][3] = in.next();
    System.out.println("shomare bime ra vared konid:");
    bimeshodeghan[i][4] = in.next();
    System.out.print("no'e bime ra vared konid:\n"
    + "a:bime rosta'ee\n"
    + "b:bime farhangyan\n"
    + "c:bime tamine ejtema'ee\n"
    + "d:bime khadamate darmani\n");
    bimeshodeghan[i][5] = in.next();
    } // end for
    while (kontrol) {
    System.out.println("Search: nam morede nazar khod ra vared konid");
    Search(bimeshodeghan, in.next(), tedad);
    System.out.println("aya mikhahid dobare search konid?{Y/N}");
    if (in.next().equals("y")) {
    kontrol = true;
    } else {
    kontrol = false;
    }
    } // end while
    System.out.println("Program ended");
    } // end main
    } // end class Bime
    آخرین ویرایش به وسیله m.a.d1992 : شنبه 01 تیر 1392 در 01:51 صبح

  6. #46
    کاربر تازه وارد آواتار m.a.d1992
    تاریخ عضویت
    دی 1391
    محل زندگی
    زاهدان
    پست
    71

    نقل قول: مجموعه برنامه‌های نوشته شده به زبان جاوا

    برنامه ی زیر مربع جادویی رو برای شما به اندازه مقداری که وارد می کنید چاپ میکننه

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package magic.table;

    import java.util.Scanner;

    public class MagicTable {

    public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    System.out.println("n ra vared konid");
    int n = s.nextInt();
    int[][] a = new int[n][n];
    int y = n * n;
    //
    for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
    a[i][j] = 0;
    }
    }
    int h = 1;
    int g = 0;
    int d = 0;
    while (h != y + 1) {
    a[g][d] = h;
    h++;
    g--;
    d--;
    if (g < 0) {
    g = n - 1;
    }
    if (d < 0) {
    d = n - 1;
    }
    if (a[g][d] > 0) {
    g = g + 1;
    if (g > n - 1) {
    g = 1;
    }
    d++;
    d++;
    if (d > n - 1) {
    d = 1;
    }
    }
    }
    for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
    System.out.print(a[i][j] + " ");
    }
    System.out.println();
    }
    System.out.println("Program ended (By: abdolsalam dehvari)");
    }
    }

  7. #47
    کاربر تازه وارد آواتار m.a.d1992
    تاریخ عضویت
    دی 1391
    محل زندگی
    زاهدان
    پست
    71

    نقل قول: مجموعه برنامه‌های نوشته شده به زبان جاوا

    برنامه زیر یه عدد حدس میزنه و بعدش از شما یه عدد میگره و به شما میگه عدد وارده ی شما که حدس شما می باشد از عددی که توسط برنامه در نظر گرفته شده کوچیک تره یا بزرگ تر، این کار اونقد ادامه پیدا میکنه تا عدد حدسی شما با عددی که برنامه در نظر گرفته یکی بشه اون وخ برنامه پیغام مناسبی چاپ میکنه

    /*
    * Random Game Number
    * By: Abdolsalam Dehvari
    */
    package randomgame;

    import javax.swing.*;
    import java.lang.Math.*;
    //
    public class RandomGame {

    public static void main(String[] args) {
    final int max = 100, min = 0;
    int result;
    int randomNumber = (int) Math.floor(
    Math.random() * (max - min + 1) + min);
    //
    result = Integer.parseInt(
    JOptionPane.showInputDialog("Enter your suggest"));
    //
    while (result != randomNumber) {
    if (result > randomNumber) {
    JOptionPane.showMessageDialog(
    null, "your suggest is more than magic number");
    }
    if (result < randomNumber) {
    JOptionPane.showMessageDialog(
    null, "your suggest is lower than magic number");
    }
    result = Integer.parseInt(
    JOptionPane.showInputDialog("Enter your suggest"));
    }
    if (result == randomNumber) {
    JOptionPane.showMessageDialog(null, "you are win \n"
    + "the magic number is: "
    + randomNumber);
    }
    }
    }

  8. #48
    کاربر تازه وارد آواتار m.a.d1992
    تاریخ عضویت
    دی 1391
    محل زندگی
    زاهدان
    پست
    71

    نقل قول: مجموعه برنامه‌های نوشته شده به زبان جاوا

    امیدوارم این چند برنامه ای که براتوون گذاتشم مفید باشه
    ایشاالله سعی می کنم برنامه های بیشتری بزارم

  9. #49
    کاربر تازه وارد آواتار m.a.d1992
    تاریخ عضویت
    دی 1391
    محل زندگی
    زاهدان
    پست
    71

    نقل قول: مجموعه برنامه‌های نوشته شده به زبان جاوا

    سلام دوستان
    چند وخ پیش که تازه استادمون به ما جاوا تدریس می کرد و همین جا ازش تشکر می کنم که جاوا رو به آموخت واقعا استاد خوبی بود
    خلاصه ایشون سره کلاساشون مثال آماده توضیح می دادن که مطالب بیشتر جا بیافته، مثال های شی گرایی، وراثت، چندریختی، واسط ها و ... که من تصمیم گرفتم این مثال ها رو بزارم براتوون امید وارم مفید باشه

    اولین مثال:
    با عنوان composition

    /*
    *
    * AbdolsalamDehvari mad
    */
    public class SprinklerSystem {

    private String valve1, valve2, valve3, valve4;
    private WaterSource source = new WaterSource();
    private int i;
    private float f;
    @Override
    public String toString() {
    return
    "valve1 = " + valve1 + " " +
    "valve2 = " + valve2 + " " +
    "valve3 = " + valve3 + " " +
    "valve4 = " + valve4 + "\n" +
    "i = " + i + " " + "f = " + f + " " +
    "source = " + source;}

    public static void main(String[] args) {
    SprinklerSystem sprinklers = new SprinklerSystem();
    System.out.println(sprinklers);
    }

    }


    /*
    *
    * AbdolsalamDehvari mad
    */
    class WaterSource {

    private String s;

    WaterSource() {
    System.out.println("WaterSource()");
    s = "Constructed";
    }

    @Override
    public String toString() {
    return s;
    }
    }

  10. #50
    کاربر تازه وارد آواتار m.a.d1992
    تاریخ عضویت
    دی 1391
    محل زندگی
    زاهدان
    پست
    71

    نقل قول: مجموعه برنامه‌های نوشته شده به زبان جاوا

    مثال دوم:
    با عنوان Object Initialization

    /*
    *
    * AbdolsalamDehvari mad
    */

    public class Bath {
    // Initializing at point of definition

    private String s1 = "Happy", s2 = "Happy", s3, s4;
    private Soap castille;
    private int i;
    private float toy;

    public Bath() {
    System.out.println("Inside Bath()");
    s3 = "Joy";
    toy = 3.14f;
    castille = new Soap();
    }
    // Instance initialization:

    {
    i = 47;
    }

    @Override
    public String toString() {
    if (s4 == null)// Delayed initialization:
    {
    s4 = "Joy";
    }
    return "s1 = " + s1 + "\n" + "s2 = " + s2 + "\n" + "s3 = " + s3 + "\n"
    + "s4 = " + s4 + "\n"
    + "i = " + i + "\n"
    + "toy = " + toy + "\n"
    + "castille = " + castille;
    }

    public static void main(String[] args) {
    Bath b = new Bath();
    System.out.println(b);
    }
    }


    /*
    *
    * AbdolsalamDehvari mad
    */
    class Soap {
    private String s;
    Soap() {
    System.out.println("Soap()");
    s = "Constructed";
    }
    public String toString() { return s; }
    }

  11. #51
    کاربر تازه وارد آواتار m.a.d1992
    تاریخ عضویت
    دی 1391
    محل زندگی
    زاهدان
    پست
    71

    نقل قول: مجموعه برنامه‌های نوشته شده به زبان جاوا

    مثال سوم:
    با عنوان Inheritance 1 یا همون وراثت البته قسمت اولش

    /*
    *
    * AbdolsalamDehvari mad
    */
    class Cleanser {

    private String s = "Cleanser";

    public void append(String a) {
    s += a;
    }

    public void dilute() {
    append(" dilute()");
    }

    public void apply() {
    append(" apply()");
    }

    public void scrub() {
    append(" scrub()");
    }

    public String toString() {
    return s;
    }

    public static void main(String[] args) {
    Cleanser x = new Cleanser();
    x.dilute();
    x.apply();
    x.scrub();
    System.out.println(x);
    }
    }



    /*
    *
    * AbdolsalamDehvari mad
    */
    public class Detergent extends Cleanser {
    // Change a method:

    @Override
    public void scrub() {
    append(" Detergent.scrub()");
    super.scrub(); // Call base-class version
    }
    // Add methods to the interface:

    public void foam() {
    append(" foam()");
    }
    // Test the new class:

    public static void main(String[] args) {
    Detergent x = new Detergent();
    x.dilute();
    x.apply();
    x.scrub();
    x.foam();
    System.out.println(x);
    System.out.println("Testing base class:");
    Cleanser.main(args);
    }
    }

  12. #52
    کاربر تازه وارد آواتار m.a.d1992
    تاریخ عضویت
    دی 1391
    محل زندگی
    زاهدان
    پست
    71

    نقل قول: مجموعه برنامه‌های نوشته شده به زبان جاوا

    سلام خدمت همه دوستان، امیدوارم تا به اینجا از برنامه هایی که گذاشتم لذت برده باشید

    برنامه ای که الان براتتون میزازم زو از یه سایت خارجی که دقیقا اسمش رو نمی دونم چی بود چند وخ پبش گرفتم و کارش اینه که لیست تمامی اتصالات شبکه ی شما رو به همراه آی پی و اطلاعات دیگه بهتون نشون میده، امیدورام بتونه مفید باشه

    /*
    * AbdolsalamDehvari mad
    */
    package javaapplication32;

    import java.io.*;
    import java.net.*;
    import java.util.*;
    import static java.lang.System.out;
    import java.util.logging.Level;
    import java.util.logging.Logger;

    /**
    *
    * @author Administrator
    */
    public class JavaApplication32 {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    try {
    Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
    for (NetworkInterface netint : Collections.list(nets)) {
    displayInterfaceInformation(netint);
    }
    } catch (SocketException ex) {
    System.err.println("error");
    }
    }

    static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
    out.printf("Display name: %s\n", netint.getDisplayName());
    out.printf("Name: %s\n", netint.getName());
    Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
    for (InetAddress inetAddress : Collections.list(inetAddresses)) {
    out.printf("InetAddress: %s\n", inetAddress);
    }
    out.printf("\n");
    }
    }

  13. #53
    کاربر تازه وارد آواتار m.a.d1992
    تاریخ عضویت
    دی 1391
    محل زندگی
    زاهدان
    پست
    71

    نقل قول: مجموعه برنامه‌های نوشته شده به زبان جاوا

    سلام
    امروز میخوام ادامه ی مثال ها رو براتوون بزارم
    مثال چهارم:
    عنوان: ادامه ی وراثت

    /*
    * AbdolslamDehvari mad
    */
    class Game {

    Game(int i) {
    System.out.println("Game constructor");
    }
    }

    class BoardGame extends Game {

    BoardGame(int i) {
    super(i);//must be first
    System.out.println("BoardGame constructor");
    }
    }

    public class Chess extends BoardGame {

    Chess() {
    super(11);
    System.out.println("Chess constructor");
    }

    public static void main(String[] args) {
    Chess x = new Chess();
    }
    }

  14. #54
    کاربر تازه وارد آواتار m.a.d1992
    تاریخ عضویت
    دی 1391
    محل زندگی
    زاهدان
    پست
    71

    نقل قول: مجموعه برنامه‌های نوشته شده به زبان جاوا

    مثال پنجم:
    عنوان: وراثت و ترکیب

    /*
    * AbdolslamDehvari mad
    */

    class Plate {

    Plate(int i) {
    System.out.println("Plate constructor");
    }
    }

    class DinnerPlate extends Plate {

    DinnerPlate(int i) {
    super(i);
    System.out.println("DinnerPlate constructor");
    }
    }

    class Utensil {

    Utensil(int i) {
    System.out.println("Utensil constructor");
    }
    }

    class Spoon extends Utensil {

    Spoon(int i) {
    super(i);
    System.out.println("Spoon constructor");
    }
    }

    class Fork extends Utensil {

    Fork(int i) {
    super(i);
    System.out.println("Fork constructor");
    }
    }

    class Knife extends Utensil {

    Knife(int i) {
    super(i);
    System.out.println("Knife constructor");
    }
    }
    // A cultural way of doing something:
    class Custom {

    Custom(int i) {
    System.out.println("Custom constructor");
    }
    }

    public class PlaceSetting extends Custom {

    private Spoon sp;
    private Fork frk;
    private Knife kn;
    private DinnerPlate pl;

    public PlaceSetting(int i) {
    super(i + 1);
    sp = new Spoon(i + 2);
    frk = new Fork(i + 3);
    kn = new Knife(i + 4);
    pl = new DinnerPlate(i + 5);
    System.out.println("PlaceSetting constructor");
    }

    public static void main(String[] args) {
    PlaceSetting x = new PlaceSetting(9);
    }
    }

  15. #55
    کاربر تازه وارد آواتار m.a.d1992
    تاریخ عضویت
    دی 1391
    محل زندگی
    زاهدان
    پست
    71

    نقل قول: مجموعه برنامه‌های نوشته شده به زبان جاوا

    مثال ششم:
    عوان: پایان وراثت


    /*
    * AbdolslamDehvari mad
    */
    class Villain {
    private String name;
    protected void set(String nm) { name = nm; }
    public Villain(String name) { this.name = name; }
    public String toString() {
    return "I’m a Villain and my name is " + name;
    }
    }
    public class Orc extends Villain {
    private int orcNumber;
    public Orc(String name, int orcNumber) {
    super(name);
    this.orcNumber = orcNumber;
    }
    public void change(String name, int orcNumber) {
    set(name); // Available because it’s protected
    this.orcNumber = orcNumber;
    }
    public String toString() {
    return "Orc " + orcNumber + ": " + super.toString();
    }
    public static void main(String[] args) {
    Orc orc = new Orc("reza", 12);
    System.out.println(orc);
    Orc s = new Orc("d",12);
    s.change("sa",12 );
    System.out.println(orc);
    }
    }

  16. #56
    کاربر تازه وارد آواتار m.a.d1992
    تاریخ عضویت
    دی 1391
    محل زندگی
    زاهدان
    پست
    71

    نقل قول: مجموعه برنامه‌های نوشته شده به زبان جاوا

    مثال هفتم:
    عنوان: upcasting

    /*
    * AbdolslamDehvari mad
    */
    class Instrument {
    public void play() {}
    static void tune( Instrument i ) {
    // ...
    i.play();
    }
    }
    // Wind objects are instruments
    // because they have the same interface:
    public class Wind extends Instrument {

    public static void main(String[] args) {
    Wind flute = new Wind();
    Instrument in = new Instrument();
    Instrument.tune(in);
    Instrument.tune(flute); // Upcasting
    }
    }

  17. #57

    نقل قول: مجموعه برنامه‌های نوشته شده به زبان جاوا

    سلام یه ماشین حساب خیلی ساده با ظاهر گرافیکی
    import java.awt.EventQueue;

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JButton;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JTextField;
    import java.awt.Font;


    public class Main extends JFrame {

    /**
    *
    */
    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private JTextField txtScreen;
    private double result = 0;

    private enum Action {
    nothing,sum,sub,div,mul
    };

    private Action lastAction = Action.nothing;
    /**
    * Launch the application.
    */
    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    try {
    Main frame = new Main();
    frame.setVisible(true);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    });
    }

    /**
    * Create the frame.
    */
    public Main() {
    setResizable(false);
    setTitle("Basic Calculator");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(200, 200, 266, 290);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JButton btn1 = new JButton("1");
    btn1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    if (txtScreen.getText().equals("0") || txtScreen.getText().equals("0.0")){
    txtScreen.setText("");
    }
    txtScreen.setText(txtScreen.getText() + "1");
    }
    });
    btn1.setBounds(10, 171, 50, 29);
    contentPane.add(btn1);

    JButton btn2 = new JButton("2");
    btn2.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    if (txtScreen.getText().equals("0") || txtScreen.getText().equals("0.0")){
    txtScreen.setText("");
    }
    txtScreen.setText(txtScreen.getText() + "2");
    }
    });
    btn2.setBounds(72, 171, 50, 29);
    contentPane.add(btn2);

    JButton btn3 = new JButton("3");
    btn3.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    if (txtScreen.getText().equals("0") || txtScreen.getText().equals("0.0")){
    txtScreen.setText("");
    }
    txtScreen.setText(txtScreen.getText() + "3");
    }
    });
    btn3.setBounds(132, 171, 50, 29);
    contentPane.add(btn3);

    JButton btn6 = new JButton("6");
    btn6.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    if (txtScreen.getText().equals("0") || txtScreen.getText().equals("0.0")){
    txtScreen.setText("");
    }
    txtScreen.setText(txtScreen.getText() + "6");
    }
    });
    btn6.setBounds(132, 131, 50, 29);
    contentPane.add(btn6);

    JButton btn5 = new JButton("5");
    btn5.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    if (txtScreen.getText().equals("0") || txtScreen.getText().equals("0.0")){
    txtScreen.setText("");
    }
    txtScreen.setText(txtScreen.getText() + "5");
    }
    });
    btn5.setBounds(72, 131, 50, 29);
    contentPane.add(btn5);

    JButton btn4 = new JButton("4");
    btn4.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    if (txtScreen.getText().equals("0") || txtScreen.getText().equals("0.0")){
    txtScreen.setText("");
    }
    txtScreen.setText(txtScreen.getText() + "4");
    }
    });
    btn4.setBounds(10, 131, 50, 29);
    contentPane.add(btn4);

    JButton btn9 = new JButton("9");
    btn9.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    if (txtScreen.getText().equals("0") || txtScreen.getText().equals("0.0")){
    txtScreen.setText("");
    }
    txtScreen.setText(txtScreen.getText() + "9");
    }
    });
    btn9.setBounds(132, 91, 50, 29);
    contentPane.add(btn9);

    JButton btn8 = new JButton("8");
    btn8.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    if (txtScreen.getText().equals("0") || txtScreen.getText().equals("0.0")){
    txtScreen.setText("");
    }
    txtScreen.setText(txtScreen.getText() + "8");
    }
    });
    btn8.setBounds(72, 91, 50, 29);
    contentPane.add(btn8);

    JButton btn7 = new JButton("7");
    btn7.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    if (txtScreen.getText().equals("0") || txtScreen.getText().equals("0.0")){
    txtScreen.setText("");
    }
    txtScreen.setText(txtScreen.getText() + "7");
    }
    });
    btn7.setBounds(10, 91, 50, 29);
    contentPane.add(btn7);

    JButton btnEqual = new JButton("=");
    btnEqual.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    switch (lastAction){
    case sum : result += Double.parseDouble(txtScreen.getText()); break;
    case sub : result -= Double.parseDouble(txtScreen.getText()); break;
    case div :
    if (txtScreen.getText().equals("0") || txtScreen.getText().equals("0.0")){
    txtScreen.setText("Error");
    return;
    }
    result /= Double.parseDouble(txtScreen.getText());
    break;
    case mul : result *= Double.parseDouble(txtScreen.getText()); break;
    default : break;
    }
    lastAction = Action.nothing;
    txtScreen.setText(String.valueOf(result));
    }
    });
    btnEqual.setBounds(132, 211, 50, 29);
    contentPane.add(btnEqual);

    JButton btnDot = new JButton(".");
    btnDot.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    String thisText = txtScreen.getText();
    if (!thisText.contains(".")){
    txtScreen.setText(txtScreen.getText() + ".");
    }
    }
    });
    btnDot.setBounds(72, 211, 50, 29);
    contentPane.add(btnDot);

    JButton btn0 = new JButton("0");
    btn0.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    if (txtScreen.getText().equals("0") || txtScreen.getText().equals("0.0")){
    txtScreen.setText("");
    }
    txtScreen.setText(txtScreen.getText() + "0");
    }
    });
    btn0.setBounds(10, 211, 50, 29);
    contentPane.add(btn0);

    txtScreen = new JTextField();
    txtScreen.setText("0");
    txtScreen.setFont(new Font("Tahoma", Font.PLAIN, 14));
    txtScreen.setBounds(10, 11, 232, 29);
    contentPane.add(txtScreen);
    txtScreen.setColumns(10);

    JButton btnMul = new JButton("*");
    btnMul.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    if (!String.valueOf(result).equals(txtScreen.getText( ))){
    result *= Double.parseDouble(txtScreen.getText());
    }
    txtScreen.setText("0");
    lastAction = Action.mul;
    }
    });
    btnMul.setBounds(192, 91, 50, 29);
    contentPane.add(btnMul);

    JButton btnDivide = new JButton("/");
    btnDivide.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    if (txtScreen.getText().equals("0") || txtScreen.getText().equals("0.0")){
    txtScreen.setText("Error");
    return;
    }
    if (!String.valueOf(result).equals(txtScreen.getText( ))){
    result /= Double.parseDouble(txtScreen.getText());
    }
    txtScreen.setText("0");
    lastAction = Action.div;
    }
    });
    btnDivide.setBounds(192, 131, 50, 29);
    contentPane.add(btnDivide);

    JButton btnSub = new JButton("-");
    btnSub.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    if (!String.valueOf(result).equals(txtScreen.getText( ))){
    result -= Double.parseDouble(txtScreen.getText());
    }
    txtScreen.setText("0");
    lastAction = Action.sub;
    }
    });
    btnSub.setBounds(192, 171, 50, 29);
    contentPane.add(btnSub);

    JButton btnSum = new JButton("+");
    btnSum.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    if (!String.valueOf(result).equals(txtScreen.getText( ))){
    result += Double.parseDouble(txtScreen.getText());
    }
    txtScreen.setText("0");
    lastAction = Action.sum;

    }
    });
    btnSum.setBounds(192, 211, 50, 29);
    contentPane.add(btnSum);

    JButton btnClear = new JButton("Clear Screen");
    btnClear.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    txtScreen.setText("0");
    result = 0;
    }
    });
    btnClear.setBounds(10, 51, 112, 29);
    contentPane.add(btnClear);

    JButton btnBackspace = new JButton("Backspace");
    btnBackspace.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    String thisText = txtScreen.getText();
    if (!thisText.equals("")){
    thisText = thisText.substring(0, thisText.length()-1);
    if (thisText.equals("")){
    thisText = "0";
    }
    } else {
    thisText = "0";
    }
    txtScreen.setText(thisText);
    }
    });
    btnBackspace.setBounds(132, 51, 112, 29);
    contentPane.add(btnBackspace);
    }
    }

    اگه ایرادی داشت خودتون زحمتشو بکشید،با عرض شرمندگی زیاد روش کار نکردم

  18. #58

    نقل قول: مجموعه برنامه‌های نوشته شده به زبان جاوا

    سلام
    استفاده از ArrayList و HashMap و Iterator برای ایجاد آرایه با طول پویا و چاپ مقادیر

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Set;



    public class Test {


    public static void main(String[] args) {

    ArrayList<String> list = new ArrayList<String>();
    list.add("A");
    list.add("H");
    list.add("S");
    System.out.println(list); //output : [A, H, S]
    list.remove(1);
    System.out.println(list); //output : [A, S]
    System.out.println(list.get(1)); //output : S
    System.out.println(list.indexOf("S")); //output : 1

    Iterator<String> li = list.listIterator();
    while (li.hasNext()){
    System.out.print(li.next() + " | "); //output : A | S |
    }

    HashMap<Integer, String> hm = new HashMap<Integer, String>();
    hm.put(10, "Afshin");
    hm.put(20, "Honari");
    hm.put(30, "A:H:S");
    System.out.println(hm); //output : {20=Honari, 10=Afshin, 30=A:H:S}
    hm.remove(30);
    System.out.println(hm); //output : {20=Honari, 10=Afshin}
    System.out.println(hm.get(10)); //output : Afshin

    Set<Integer> hms = hm.keySet();
    Iterator<Integer> hmi = hms.iterator();
    while(hmi.hasNext()){
    int str = hmi.next();
    System.out.println(str + " " + hm.get(str)); //output : 20 Honari /n 10 Afshin
    }
    }

    }

    آخرین ویرایش به وسیله AfshinHonari : پنج شنبه 13 تیر 1392 در 00:31 صبح

  19. #59

    نقل قول: مجموعه برنامه‌های نوشته شده به زبان جاوا

    رسم نمودار به صورت گرافیکی با تعیین طول بازه
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    public class rasme_nomodar extends JFrame{
    private static final long serialVersionUID = 1L;
    private JTextArea zz;
    private JTextField z,xx;
    JLabel m,n;
    private JButton btn;
    private Boolean boo=false;
    public void paint(Graphics g)
    {

    super.paint(g);
    g.setColor(Color.WHITE);
    g.drawLine(400,25,400,597);
    g.drawLine(0, 300, 800, 300);
    if(boo==true){
    for(int i=0;i<=(int)strtodou(z.getText());i++){
    String zz="("+xx.getText()+")";
    int s=(int) parant(zz,i);
    int ss=(int)parant(zz,i+1);
    g.setColor(Color.red);
    g.drawLine(i*2+401, 301-s,401+(( i+1)*2), 301-ss);

    }
    for(int i=-(int)strtodou(z.getText());i<=0;i++){
    String zz="("+xx.getText()+")";
    int s=(int) parant(zz,i);
    int ss=(int)parant(zz,i+1);
    g.setColor(Color.blue);
    g.drawLine(i*2+401, 301-s,401+(( i+1)*2), 301-ss);

    }
    }

    }
    public rasme_nomodar(){
    this.setTitle("Nomadar");
    this.setBounds(100 , 50 , 800 , 600);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
    this.getContentPane().setBackground(Color.BLACK);
    this.setResizable(false);
    this.setLayout(null);
    this.setVisible(true);

    zz=new JTextArea();
    zz.setText("");
    zz.setBounds(5 , 0 , 105 , 20);
    zz.setBackground(Color.white);
    this.getContentPane().add(zz);

    z=new JTextField();
    z.setBounds(5,55, 100, 20);
    z.setForeground(Color.green);
    z.setText("Enter Mahdode");
    z.setEnabled(false);
    z.addMouseListener(new MouseListener() {
    public void mouseReleased(MouseEvent arg0) {

    }

    public void mousePressed(MouseEvent arg0) {

    }

    public void mouseExited(MouseEvent arg0) {

    }

    public void mouseEntered(MouseEvent arg0) {
    }
    public void mouseClicked(MouseEvent arg0) {
    z.setEnabled(true);
    z.setText(null);
    }
    });
    this.getContentPane().add(z);

    xx=new JTextField();
    xx.setBounds(5,30, 100, 20);
    xx.setForeground(Color.red);
    xx.setText("Enter X");
    xx.setEnabled(false);
    xx.addMouseListener(new MouseListener() {
    public void mouseReleased(MouseEvent arg0) {

    }

    public void mousePressed(MouseEvent arg0) {

    }

    public void mouseExited(MouseEvent arg0) {

    }

    public void mouseEntered(MouseEvent arg0) {
    }
    public void mouseClicked(MouseEvent arg0) {
    xx.setEnabled(true);
    xx.setText(null);
    }
    });
    this.getContentPane().add(xx);

    btn=new JButton();
    btn.setText("DRAW");
    btn.setBounds(115, 0, 100, 20);
    this.getContentPane().add(btn);
    btn.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
    press(e);

    }
    });


    this.addMouseListener(new MouseListener() {
    public void mouseReleased(MouseEvent arg0) {
    }
    public void mousePressed(MouseEvent arg0) {
    }
    public void mouseExited(MouseEvent arg0) {
    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
    }

    @Override
    public void mouseClicked(MouseEvent arg0) {
    }
    });
    this.addMouseMotionListener(new MouseMotionListener() {
    @Override
    public void mouseMoved(MouseEvent e) {
    mousemove(e);
    }

    @Override
    public void mouseDragged(MouseEvent arg0) {

    }
    });
    }
    public void press(ActionEvent e){
    this.repaint();
    boo=true;
    paint(null);
    boolean iscontinue=true;
    if (z.getText().equals(""))
    {
    z.setBackground(Color.red);
    iscontinue=false;
    }
    else
    z.setBackground(Color.WHITE);
    if(xx.getText().equals("")){
    xx.setBackground(Color.red);
    iscontinue=false;
    }
    else
    xx.setBackground(Color.WHITE);
    if (!iscontinue)
    {
    JOptionPane.showMessageDialog(null , "Please Enter Red Fields");
    return;
    }
    }

    public static void main(String[] args)
    {
    JFrame.setDefaultLookAndFeelDecorated(true);
    new rasme_nomodar();
    }
    public void mousemove(MouseEvent e){
    int i=e.getX()-400;
    int ii=300-e.getY();
    zz.setText("Moved to ("+i+","+ii+")");
    }
    public static double strtodou(String e)
    {
    int i = 0,x = 1, d = 1;
    int a = 0;
    if(e.charAt(i)=='-')
    {
    d = -1;
    i++;
    }
    if(e.charAt(i)=='+')
    i++;
    double r = 0;
    while(i<e.length())
    {
    r = r*10+(e.charAt(i++)-'0');
    if(i==e.length())
    break;
    if(x>1)
    x*=10;
    if(e.charAt(i)=='.')
    {
    i++;
    x*=10;
    }
    if(x>1000000)
    {
    a ++;
    break;
    }
    }
    if(a>0)
    x/=10;
    return (r/(x*1.0))*d;
    }//end of strtodou

    public int mt=1;
    ////////////////////////////////////Math/////////////////////////////////////
    public double exp(double x){
    double s=1;
    for(int i=0;i<=1000;i++){
    double k=1;
    for(int j=1;j<=i;j++){
    k*=(x*1.0/j);
    }
    s+=k;
    }
    return s;
    }
    //***********************************//
    public double sin(double x){
    int a=-1;
    x=Math.toRadians(x);
    double s=x;
    for(int i=3;i<=1000;i+=2){
    double k=1;
    for(int j=1;j<=i;j++){
    k*=(x*1.0/j);
    }
    s+=k*a;
    a*=-1;
    }
    return s;
    }
    //***********************************//
    public double cos(double x){
    int a=-1;
    x=Math.toRadians(x);
    double s=1;
    for(int i=2;i<=1000;i+=2){
    double k=1;
    for(int j=1;j<=i;j++){
    k*=(x*1.0/j);
    }
    s+=k*a;
    a*=-1;
    }
    return s;
    }
    //***********************************//


    /////////////////////////////////////////////////////////////////////////////
    public double toDouble(String s){
    double n=0;

    for(int i=0;i<s.length();i++){
    if(s.charAt(i)>='0'&&s.charAt(i)<='9'){
    int p=1;
    boolean as = false;
    int t=10;
    n=s.charAt(i)-'0';
    while(i+p<s.length()){
    if((s.charAt(i+p)>='0'&&s.charAt(i+p)<='9')){
    if(!as){n=n*10+s.charAt(i+p)-'0';}
    if(as){n=n+(s.charAt(i+p)-'0')*1.0/t;t*=10;}
    p++;
    }
    else if(s.charAt(i+p)=='.'){
    as=true;p++;
    }
    else break;

    }
    i+=p-1;
    continue;
    }
    }
    if(s.charAt(0)=='-')n=-n;
    return n;
    }
    public double toarr(String s,double x)
    {

    double a[]=new double[s.length()];
    char b[] = new char [s.length()];
    int j=0;
    for(int i=0;i<s.length();i++){
    if(s.charAt(i)>='0'&&s.charAt(i)<='9'){
    int p=1;
    boolean as = false;
    boolean na = false;
    int t=10;
    double n=s.charAt(i)-'0';
    if(i>0)
    if(s.charAt(i-1)=='-'){
    if(i-2>0){
    if(!(s.charAt(i-2)>='0'&&s.charAt(i-2)<='9')&&!(s.charAt(i-2)=='x')){na=true;}
    }
    else if(i-2==-1)
    na=true;
    }
    while(i+p<s.length()){
    if((s.charAt(i+p)>='0'&&s.charAt(i+p)<='9')){
    if(!as)n=n*10+s.charAt(i+p)-'0';
    if(as){n=n+(s.charAt(i+p)-'0')*1.0/t;t*=10;}
    p++;
    }
    else if(s.charAt(i+p)=='.'){
    as=true;p++;
    }
    else break;

    }
    i+=p-1;
    a[j]=n;
    b[j]='_';
    if(na){a[j]*=-1;shifta(a,j-1,1);shiftb(b,j-1,1);j--;}
    j++;
    continue;
    }
    if(s.charAt(i)=='+'){
    b[j++]='+';
    continue;
    }
    if(s.charAt(i)=='x'){
    b[j++]='x';
    continue;
    }
    if(s.charAt(i)=='^'){
    b[j++]='^';
    continue;
    }
    if(s.charAt(i)=='-'){

    b[j++]='-';
    continue;
    }
    if(s.charAt(i)=='*'){
    b[j++]='*';
    continue;
    }
    if(s.charAt(i)=='/'){
    b[j++]='/';
    continue;
    }
    if(s.charAt(i)=='s'){
    if(i+2<s.length())
    if(s.charAt(i+1)=='i'&&s.charAt(i+2)=='n')
    {b[j++]='s';i+=2;continue;}
    }
    if(s.charAt(i)=='c'){
    if(i+2<s.length())
    if(s.charAt(i+1)=='o'&&s.charAt(i+2)=='s')
    {b[j++]='c';i+=2;continue;}
    }
    if(s.charAt(i)=='t'){
    if(i+2<s.length())
    if(s.charAt(i+1)=='a'&&s.charAt(i+2)=='n')
    {b[j++]='t';i+=2;continue;}
    }
    if(s.charAt(i)=='c'){
    if(i+2<s.length())
    if(s.charAt(i+1)=='o'&&s.charAt(i+2)=='t')
    {b[j++]='g';i+=2;continue;}
    }
    if(s.charAt(i)=='l'){
    if(i+2<s.length())
    if(s.charAt(i+1)=='o'&&s.charAt(i+2)=='g')
    {b[j++]='l';i+=2;continue;}
    }
    if(s.charAt(i)=='s'){
    if(i+2<s.length())
    if(s.charAt(i+1)=='q'&&s.charAt(i+2)=='r')
    {b[j++]='j';i+=2;continue;}
    }
    if(s.charAt(i)=='e'){
    if(i+2<s.length())
    if(s.charAt(i+1)=='x'&&s.charAt(i+2)=='p')
    {b[j++]='e';i+=2;continue;}
    }
    }
    double r=calc(j,a,b,x);
    return r;

    }
    public void shifta(double a[] ,int p,int l){
    for(int i=p;i<a.length-l;i++)
    {
    if(i+l>=a.length)break;
    a[i]=a[i+l];
    }
    }
    public void shiftb(char b[],int p,int l){
    for(int i=p;i<b.length-l;i++)
    {
    if(i+l>=b.length)break;
    b[i]=b[i+l];
    }
    }
    public double calc(int j,double a[],char b[],double x){
    for(int i=0;i<b.length;i++)
    if(b[i]=='x'){
    b[i]='_';
    a[i]=x;
    }

    for(int i=0;i<b.length;i++)
    if(b[i]=='^'){
    if(a[i+1]%2==0&&mt%2==1)
    mt=(int)a[i+1];
    }
    while(j>1){
    for(int i=0;i<j-1;i++){
    if(b[i]=='^'){
    double t=a[i-1];
    for(int ii=2;ii<=a[i+1];ii++)a[i-1]*=t;
    shifta(a,i,2);shiftb(b,i,2);j-=2;continue;
    }

    }
    for(int i=0;i<j;i++){
    if(b[i]=='s'){
    a[i]=sin(a[i+1]);shifta(a,i+1,1);shiftb(b,i,1);j--;continue;
    }
    if(b[i]=='c'){
    a[i]=cos(a[i+1]);shifta(a,i+1,1);shiftb(b,i,1);j--;continue;
    }
    if(b[i]=='t'){
    a[i+1]=Math.toRadians(a[i+1]);
    a[i]=Math.tan(a[i+1]);shifta(a,i+1,1);shiftb(b,i,1);j--;continue;
    }
    if(b[i]=='l'){
    a[i]=Math.log(a[i+1]);shifta(a,i+1,1);shiftb(b,i,1);j--;continue;
    }
    if(b[i]=='e'){
    a[i]=exp(a[i+1]);shifta(a,i+1,1);shiftb(b,i,1);j--;continue;
    }
    if(b[i]=='j'){
    a[i]=Math.sqrt(a[i+1]);shifta(a,i+1,1);shiftb(b,i,1);j--;continue;
    }

    }
    for(int i=0;i<j-1;i++){
    if(b[i]=='*'){
    a[i-1]=a[i-1]*a[i+1];shifta(a,i,2);shiftb(b,i,2);j-=2;continue;
    }
    if(b[i]=='/'){
    a[i-1]=a[i-1]/a[i+1];shifta(a,i,2);shiftb(b,i,2);j-=2;continue;
    }
    }
    for(int i=0;i<j-1;i++){
    if(b[i]=='+'){
    a[i-1]=a[i-1]+a[i+1];shifta(a,i,2);shiftb(b,i,2);j-=2;continue;
    }
    if(b[i]=='-'){
    a[i-1]=a[i-1]-a[i+1];shifta(a,i,2);shiftb(b,i,2);j-=2;continue;
    }
    }

    }
    return a[0];

    }
    public double parant(String s,double x){
    for(int i=s.length()-1;i>=0;i--){
    if(s.charAt(i)=='('){
    int j=1;
    String sp ="";
    while(s.charAt(i+j)!=')'){
    sp=sp+s.charAt(i+j);
    j++;
    }
    double k=toarr(sp,x);
    String ss="";
    for(int ii=0;ii<i;ii++) ss=ss+s.charAt(ii);
    ss=ss+k;
    for(int ii=i+j+1;ii<s.length();ii++) ss=ss+s.charAt(ii);
    s=ss;
    i=s.length()-1;
    }
    }
    double sn=toDouble(s);
    return sn;
    }
    public void Find(String s){
    System.out.println("Te'dad Risheha :"+mt);
    double a=0,aa=0;
    double b=0,bb=0;
    double k=1,kk=1;
    double i=0,ii=0;
    boolean d=false;
    int n=0;
    while(true){
    a=parant(s,i);
    aa=parant(s,0);
    b=parant(s,ii);
    bb=parant(s,0);
    if(a==0){System.out.println("Rishe: "+i);d=true;n++;}
    if(b==0&&i!=0){System.out.println("Rishe: "+ii);d=true;n++;}
    if(mt%2==1&&d)break;
    else if(n==mt)break;

    if(a*aa<0){System.out.println("Rishe: ["+0+","+i+"]");k=k/10;i-=(k*10);}
    if(bb*b<0){System.out.println("Rishe: ["+ii+","+0+"]");kk=kk/10;ii+=(kk*10);}
    i+=k;
    ii-=kk;
    }

    }

    }

  20. #60

    نقل قول: مجموعه برنامه‌های نوشته شده به زبان جاوا

    جمع چند جمله ای
    package poroje;
    import java.util.Scanner;
    public class jam_chand_jomle
    {
    public static void main(String[] args)
    {
    Scanner input=new Scanner(System.in);
    System.out.print("Enter first string:");
    String s1=input.nextLine();
    System.out.print("Enter second string:");
    String s2=input.nextLine();
    if(s2.charAt(0)!='-')
    s2='+'+s2;
    String s="";
    int is=0;
    s=s1+s2;
    if(s.charAt(0)!='-')
    s='+'+s;
    String ms[]=new String[100];
    for(int i=0;i<100;i++)
    ms[i]="";
    int cms=0;
    while(is<s.length())
    {
    ms[cms]+=s.charAt(is++);
    while((s.charAt(is)!='+')&&(s.charAt(is)!='-'))
    {
    ms[cms]+=s.charAt(is++);
    if(is>=s.length())
    break;
    }//end of second while class
    cms++;
    }//end of first while class
    int ims,z,ca=0,i,sign,t;
    int a[]=new int[250];
    for(i=0;i<cms;i++)
    {
    ims=0;
    z=0;
    t=0;
    sign=1;
    if(ms[i].charAt(ims)=='-')
    sign=-1;
    ims++;
    if((ms[i].charAt(ims)>='0')&&(ms[i].charAt(ims)<='9'))
    {
    while((ms[i].charAt(ims)>='0')&&(ms[i].charAt(ims)<='9'))
    {
    z=z*10+(ms[i].charAt(ims)-'0');
    ims++;
    if(ims>=ms[i].length())
    break;
    }//end of while class
    if(ims<ms[i].length())
    {
    ims+=2;
    if(ims>=ms[i].length())
    t=1;
    else
    {
    ims++;
    while((ms[i].charAt(ims)>='0')&&(ms[i].charAt(ims)<='9'))
    {
    t=t*10+(ms[i].charAt(ims)-'0');
    ims++;
    if(ims>=ms[i].length())
    break;
    }//end of while class
    }//end of else class
    }//end of if class
    }//end of if class
    else if(ms[i].charAt(ims)=='x')
    {
    z=1;
    ims++;
    if(ims>=ms[i].length())
    t=1;
    else
    {
    ims++;
    while((ms[i].charAt(ims)>='0')&&(ms[i].charAt(ims)<='9'))
    {
    t=t*10+(ms[i].charAt(ims)-'0');
    ims++;
    if(ims>=ms[i].length())
    break;
    }//end of while class
    }//end of else class
    }//end of else if class
    z=z*sign;
    a[t]+=z;
    if(t>ca)
    ca=t;
    }//end of for class
    for(i=99;i>=2;i--)
    {
    if(a[i]!=0)
    {
    if(a[i]<0)
    System.out.print(a[i]+"x^"+i);
    else
    System.out.print("+"+a[i]+"x^"+i);
    }//end of if class
    }//end of for class
    if(a[1]!=0)
    {
    if(a[1]<0)
    System.out.print(a[1]+"x");
    else
    System.out.print("+"+a[1]+"x");
    }//end of if class
    if(a[0]!=0)
    {
    if(a[0]<0)
    System.out.print(a[0]);
    else
    System.out.print("+"+a[0]);
    }//end of for class
    }//End of main class
    }//end of class

  21. #61

    نقل قول: مجموعه برنامه‌های نوشته شده به زبان جاوا

    تفریق چند جمله ای
    package poroje;
    import java.util.Scanner;
    public class tafrige_chand_jomle
    {
    public static void main(String[] args)
    {
    Scanner input=new Scanner(System.in);
    System.out.print("Enter first string:");
    String s1=input.nextLine();
    System.out.print("enter second string:");
    String s2=input.nextLine();
    if(s2.charAt(0)!='-')
    s2='+'+s2;
    String s3="";
    for(int i=0;i<s2.length();i++)
    {
    if((s2.charAt(i)!='+')&&(s2.charAt(i)!='-'))
    s3+=s2.charAt(i);
    if(s2.charAt(i)=='+')
    s3+='-';
    if(s2.charAt(i)=='-')
    s3+='+';
    }//end of for class
    String s="";
    int is=0;
    s=s1+s3;
    if(s.charAt(0)!='-')
    s='+'+s;
    String ms[]=new String[100];
    for(int i=0;i<100;i++)
    ms[i]="";//55*x^6+23//54*x^6+x-x^2-2
    int cms=0;
    while(is<s.length() )
    {
    ms[cms]+=s.charAt(is++);
    while((s.charAt(is)!='+')&&(s.charAt(is)!='-'))
    {
    ms[cms]+=s.charAt(is++);
    if(is>=s.length())
    break;
    }//end of second while class
    cms++;
    }//end of first while class
    int ims,z,ca=0,i,sign,t;
    int a[]=new int[250];
    for(i=0;i<cms;i++)
    {
    ims=0;
    z=0;
    t=0;
    sign=1;
    if(ms[i].charAt(ims)=='-')
    sign=-1;
    ims++;
    if((ms[i].charAt(ims)>='0')&&(ms[i].charAt(ims)<='9'))
    {
    while((ms[i].charAt(ims)>='0')&&(ms[i].charAt(ims)<='9'))
    {
    z=z*10+(ms[i].charAt(ims)-'0');
    ims++;
    if(ims>=ms[i].length())
    break;
    }//end of while class
    if(ims<ms[i].length())
    {
    ims+=2;
    if(ims>=ms[i].length())
    t=1;
    else
    {
    ims++;
    while((ms[i].charAt(ims)>='0')&&(ms[i].charAt(ims)<='9'))
    {
    t=t*10+(ms[i].charAt(ims)-'0');
    ims++;
    if(ims>=ms[i].length())
    break;
    }//end of while class
    }//end of else class
    }//end of if class
    }//end of if class
    else if(ms[i].charAt(ims)=='x')
    {
    z=1;
    ims++;
    if(ims>=ms[i].length())
    t=1;
    else
    {
    ims++;
    while((ms[i].charAt(ims)>='0')&&(ms[i].charAt(ims)<='9'))
    {
    t=t*10+(ms[i].charAt(ims)-'0');
    ims++;
    if(ims>=ms[i].length())
    break;
    }//end of while class
    }//end of else class
    }//end of else if class
    z=z*sign;
    a[t]+=z;
    if(t>ca)
    ca=t;
    }//end of for class
    for(i=99;i>=2;i--)
    {
    if(a[i]!=0)
    {
    if(a[i]<0)
    System.out.print(a[i]+"x^"+i);
    else
    System.out.print("+"+a[i]+"x^"+i);
    }//end of if class
    }//end of for class
    if(a[1]!=0)
    {
    if(a[1]<0)
    System.out.print(a[1]+"x");
    else
    System.out.print("+"+a[1]+"x");
    }//end of if class
    if(a[0]!=0)
    {
    if(a[0]<0)
    System.out.print(a[0]);
    else
    System.out.print("+"+a[0]);
    }//end of for class
    }//End of main class
    }//end of class class

  22. #62

    نقل قول: مجموعه برنامه‌های نوشته شده به زبان جاوا

    سلام
    یه مثال که وا3 کپیه فایل از کلاسای خود جاوا و کلاس File آپاچی استفاده میکنه
    CopyFile.zip
    بقیه کلاسا از این آدرس قابل دریافتن:
    commons.apache.org
    آخرین ویرایش به وسیله AfshinHonari : یک شنبه 16 تیر 1392 در 01:52 صبح

  23. #63

    نقل قول: مجموعه برنامه‌های نوشته شده به زبان جاوا

    سلام
    برنامه ای با استفاده از شی گرایی برای چاپ 5تا 10 خط رندوم (گرافیکی)


    package randomdraw;
    import javax.swing.JFrame;
    import javax.swing.JPanel;

    public class RandomDraw {

    public static void main(String[] args) {
    line p = new line();
    JFrame win = new JFrame();

    win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;

    win.add(p);
    win.setSize(300,300);
    win.setVisible(true);


    }
    }




    package randomdraw;

    import java.util.Random;
    import java.awt.Graphics;
    import java.awt.Color;
    import javax.swing.JPanel;

    public class line extends JPanel {

    private int x1 , x2 , y1 , y2;
    private Color c;
    private int r;
    private int[][] soal = new int[10][4];
    private Color[] clr = new Color[10];
    Random l = new Random();

    public line (){

    r = 5 + l.nextInt(6);

    for(int i=0 ; i<r ; i++)
    {

    x1 = l.nextInt(300);
    x2 = l.nextInt(300);
    y1 = l.nextInt(300);
    y2 = l.nextInt(300);

    c = new Color(l.nextInt(256), l.nextInt(256) , l.nextInt(256));

    soal[i][0] = x1;
    soal[i][1] = x2;
    soal[i][2] = y1;
    soal[i][3] = y2;
    clr[i]=c;
    }


    }


    public void paintComponent(Graphics g){

    super.paintComponent(g);

    setBackground(Color.WHITE);

    for(int i=0 ; i<r ; i++)
    { g.setColor(clr[i]);
    g.drawLine(soal[i][0],soal[i][1],soal[i][2],soal[i][3]);

    }

    }
    }

  24. #64
    کاربر دائمی آواتار mr.dp+
    تاریخ عضویت
    آبان 1392
    محل زندگی
    محلی که در آن زندگی می‌کنم.
    پست
    119

    Arrow نقل قول: مجموعه برنامه‌های نوشته شده به زبان جاوا

    سلام
    من یک برنامه خیلی ساده ساختم که میتونه برای کسانی که دارن انگلیسی یاد میگیرن مفید باشه. و همچنین یه بازی خیلی کوچیک هم محسوب میشه
    چه کاری میکنه ؟ طبق تصویر چند تا سوال ازتون میپرسه و بعدش یه داستان براتون میسازه (کاملا انگلیسی)
    بصورت کامل پروژه رو گذاشتم برای دانلود که روی NetBeans هم ساخته شده !



    لینک دانلود پروژه : Download

  25. #65

    نقل قول: مجموعه برنامه‌های نوشته شده به زبان جاوا

    با سلام وتشکر
    نمیشد همین برنامه رو یکم ساده ترو کوتاه ترتر بنویسیم؟ینی به جای اینکه کلاس searchرو بنویسیم با استفاده از تابع بازگشتی عدد اولو پیدا کنیم؟بازم ممنون

  26. #66

    نقل قول: مجموعه برنامه‌های نوشته شده به زبان جاوا

    salam man taze java ro shoru kardam ye soal kheili sade daram ye kod neveshtam ke kheily kutahe vali nemidunam chera ejra nemishe lotfan komakam konid batashakor:
    package javaapplication3;

    /**
    *
    * @author my pc
    */
    import java.util.Scanner;
    public class JavaApplication3 {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {

    System.out.println("enter 3 number");
    double num1;
    double num2;
    double num3;
    Scanner input = new Scanner(System.in);
    num1 = input.nextDouble();
    num2 = input.nextDouble();
    num3 = input.nextDouble();
    System.out.println(Math.max(num1,(num2, num3));


    }


    }


  27. #67
    کاربر دائمی آواتار mr.dp+
    تاریخ عضویت
    آبان 1392
    محل زندگی
    محلی که در آن زندگی می‌کنم.
    پست
    119

    نقل قول: مجموعه برنامه‌های نوشته شده به زبان جاوا

    نقل قول نوشته شده توسط ftmh02 مشاهده تاپیک
    salam man taze java ro shoru kardam ye soal kheili sade daram ye kod neveshtam ke kheily kutahe vali nemidunam chera ejra nemishe lotfan komakam konid batashakor:
    package javaapplication3;

    /**
    *
    * @author my pc
    */
    import java.util.Scanner;
    public class JavaApplication3 {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {

    System.out.println("enter 3 number");
    double num1;
    double num2;
    double num3;
    Scanner input = new Scanner(System.in);
    num1 = input.nextDouble();
    num2 = input.nextDouble();
    num3 = input.nextDouble();
    System.out.println(Math.max(num1,(num2, num3));


    }


    }


    متد max فقط 2 تا آرگومان میگیره !!!!
    اگه شما بخواید بیش از 2 آرگمان استفاده بکنید باید کدتون رو اینطوری بنویسید:
    System.out.println(Math.max(num1, Math.max(num2,num3)));


    و اینکه پست های اضافه ای که گذاشتید رو لطفا حذف کنید که شلوغ نشه !!!!

  28. #68
    کاربر دائمی آواتار vahid-p
    تاریخ عضویت
    آذر 1391
    محل زندگی
    تهران
    پست
    1,140

    نقل قول: مجموعه برنامه‌های نوشته شده به زبان جاوا

    تاپیک خیلی خوبیه و یکی دو ماه هست از وجودش باخبرم اما فعالیتی نداشتم و از نظر من یه اشکالی که وجود داره، ناخواسته باید محدوده ای برای برنامه هامون بذاریم. مثلا من یه بازی که با جاوا نوشته بودم رو میتونم کامل آپلود کنم، ولی مطمئنم به راحتی ازش برای پروژه درسی سوء استفاده میشه و این خط قرمزه برای من. متاسفانه برای همین مجبوریم همیشه یه تیکه خاص از برنامه ها رو بنویسیم که به تنهایی کسی نتونه ازش استفاده کنه و ارزش نمره نداشته باشه.

    حالا این انتقادم بود که البته انتقاد از شما نیست، از همه از جمله خودم است که این مشکل راه حلی براش پیدا نکردیم!
    اما برای اینکه دست خالی نیومده باشم، یه کد ساده در عین حال کاربردی ( حداقل برای پروژه من نیاز بود، گفتم این تیکش رو بذارم ).
    فکر میکنم بتونم با این پیشفرض هایی که گفتم، از این به بعد هر از گاهی یه کد بذارم، و تو این حرکت قشنگتون سهیم باشم.

    یک آدرس دایرکتوری ( یا همون پوشه یا فولدر ) رو میگیره، تا هر عمقی اسامی فایل ها و پوشه ها رو به صورت بازگشتی مینویسه. (یه نظمی هم بهش میده )
    یه وقت آدرس ندید درایو \:C که اونوقت تا همشو ننویسه ولکن ماجرا نمیشه مگر اینکه برنامه رو کرش کنید.
        public static void main(String[] args) throws IOException {
    File file=new File("C:\\Program Files\\Java\\jre8"); //Directory address
    System.out.println(file.getAbsolutePath());
    if(file.isDirectory()){
    listFile(file,0);
    }
    }
    public static void listFile(File file,int depth){
    for(File f:file.listFiles()){
    if(f.isDirectory()){
    for(int i=0;i<depth;i++) System.out.print("|"+"\t");
    System.out.println("|_"+f.getName());
    listFile(f,depth+1);
    }else{
    for(int i=0;i<depth;i++) System.out.print("|"+"\t");
    System.out.println("|_"+f.getName());
    }
    }
    }

  29. #69
    کاربر دائمی آواتار A.iman
    تاریخ عضویت
    اسفند 1391
    محل زندگی
    تبریز
    پست
    184

    نقل قول: مجموعه برنامه‌های نوشته شده به زبان جاوا

    یه مبحثی توی فیزیک قبل از اختراع کامپیوتر مطرح بود که میگه چه زمانی ماده از بالای یه جسم میتونه به پائین تراوش کنه ؟ مثلا تو عکس زیر ما هر خونه ای که داریم یا بازه(سفید) یا بسته است(سیاه) یا بازه و مایع توشه(ابی) . زمانی که از سطح بالا با استفاده از خونه های باز همجوار راهی به سطح پائین وجود داشته باشه ، میگیم که توارش انجام شده .
    percolation-250.png
    ازمایشها نشون دادن که pی استانه ای وجود دارد به گونه ای که اگر احتمال باز بودن خانه ها از ان p بیشتر باشد ، به احتمال بسیار قوی تراوش انجام خواهد شد و اگر کمتر باشد تراوشی انجام نخواهد شد . در شکل زیر این نمودار را مشاهده میکنید :
    Captیبسure.PNG
    هدف این برنامه ای که نوشتیم محاسبه این p هست . به طوری که ما ابتدا یه جدول n*n انتخاب میکنیم و تابع ایجاد اعداد شانسی رو بر روی اون لحاظ مکنیم . تا بهصورت شانسی به تعداد 1000 بار ویا بیشتر(وردی کاربر) این کار را انجام دهد و مقدار p را محاسبه کند .
    برای اینکار داد ه ساختاری را معرفی کرده ایم که بتونه این عمل رو پیاده سازیکنه . این داده ساختار مثل همبندی پویاست و عکسشو اینجا قرار میدم :کدهاشو بخونید متوجه میشید نیازی ب توضیح نیست :
    Captشسیure.PNG
    کلاس WeightedQuickUnionFind همون ساختار داده هست
    کلاس PercolationStats و Percolation عمل و شرط تراوش رو بررسی میکنند
    کلاس PercolationVisualizerهم برای گرافیکی بودن برنامه هست :
    public class WeightedQuickUnionUF {    private int[] id;    // id[i] = parent of i
    private int[] sz; // sz[i] = number of objects in subtree rooted at i
    private int count; // number of components


    // Create an empty union find data structure with N isolated sets.
    public WeightedQuickUnionUF(int N) {
    count = N;
    id = new int[N];
    sz = new int[N];
    for (int i = 0; i < N; i++) {
    id[i] = i;
    sz[i] = 1;
    }
    }


    // Return the number of disjoint sets.
    public int count() {
    return count;
    }


    // Return component identifier for component containing p
    public int find(int p) {
    while (p != id[p])
    p = id[p];
    return p;
    }


    // Are objects p and q in the same set?
    public boolean connected(int p, int q) {
    return find(p) == find(q);
    }



    // Replace sets containing p and q with their union.
    public void union(int p, int q) {
    int i = find(p);
    int j = find(q);
    if (i == j) return;


    // make smaller root point to larger one
    if (sz[i] < sz[j]) { id[i] = j; sz[j] += sz[i]; }
    else { id[j] = i; sz[i] += sz[j]; }
    count--;
    }




    public static void main(String[] args) {
    int N = StdIn.readInt();
    WeightedQuickUnionUF uf = new WeightedQuickUnionUF(N);


    // read in a sequence of pairs of integers (each in the range 0 to N-1),
    // calling find() for each pair: If the members of the pair are not already
    // call union() and print the pair.
    while (!StdIn.isEmpty()) {
    int p = StdIn.readInt();
    int q = StdIn.readInt();
    if (uf.connected(p, q)) continue;
    uf.union(p, q);
    StdOut.println(p + " " + q);
    }
    StdOut.println(uf.count() + " components");
    }


    }

    ..


    public class Percolation {
    private int N;
    private int topVS;
    private int bottomVS;
    private boolean[][] grid;
    private WeightedQuickUnionUF union;
    private WeightedQuickUnionUF PRBL;




    public Percolation(int n) {


    N = n;



    union = new WeightedQuickUnionUF((n + 1) * (n + 1) + 2);
    PRBL = new WeightedQuickUnionUF((n + 1) * (n + 1) + 2);
    topVS = (n + 1) * (n + 1);
    bottomVS = (n + 1) * (n + 1) + 1;




    for (int j = 1; j <= N; j++) {
    union.union(topVS, xyTo1D(1, j));
    PRBL.union(topVS, xyTo1D(1, j));
    }



    for (int j = 1; j <= N; j++) {
    union.union(bottomVS, xyTo1D(N, j));
    }


    grid = new boolean[N + 1][N + 1];
    }
    /**
    * Opens the site located at row i and column j.
    */
    public void open(int i, int j) {
    checkIndices(i, j);
    grid[i][j] = true;
    int site1D = xyTo1D(i, j);


    if (i < N) {
    if (grid[i + 1][j]) {
    union.union(site1D, xyTo1D(i + 1, j));
    PRBL.union(site1D, xyTo1D(i + 1, j));
    }
    } if (i > 1) {
    if (grid[i - 1][j]) {
    union.union(site1D, xyTo1D(i - 1, j));
    PRBL.union(site1D, xyTo1D(i - 1, j));
    }
    } if (j < N) {
    if (grid[i][j + 1] && j < N) {
    union.union(site1D, xyTo1D(i, j + 1));
    PRBL.union(site1D, xyTo1D(i, j + 1));
    }
    } if (j > 1) {
    if (grid[i][j - 1] && j > 0) {
    union.union(site1D, xyTo1D(i, j - 1));
    PRBL.union(site1D, xyTo1D(i, j - 1));
    }
    }
    }
    /**
    * Returns true if site (i, j) is open,
    * otherwise returns false.
    */
    public boolean isOpen(int i, int j) {
    checkIndices(i, j);
    return grid[i][j];
    }
    /**
    * Returns true if site (i, j) is full,
    * otherwise returns false.
    */
    public boolean isFull(int i, int j) {
    checkIndices(i, j);
    if (!PRBL.connected(xyTo1D(i, j), topVS)) {
    return false;
    }
    if (grid[i][j] && union.connected(xyTo1D(i, j), topVS)) {
    return true;
    }
    return false;
    }
    /**
    * Returns true if the system percolates,
    * otherwise returns false.
    */
    public boolean percolates() {
    return union.connected(topVS, bottomVS);
    }
    /**
    * Converts the position (row and column) of
    * a site to its corresponding object number.
    */
    private int xyTo1D(int i, int j) {
    return N * i + j;
    }
    /**
    * Checks if the site (i, j) exists in the grid.
    * If it does not exist, throws an exception.
    */


    private void checkIndices(int i, int j) {
    if (i > N || i < 1) {
    new IndexOutOfBoundsException("Row index i out of bounds");
    } if (j > N || j < 1) {
    new IndexOutOfBoundsException("Column index j out of bounds");
    }


    }
    // test client
    public static void main(String[] args) {
    int N = Integer.parseInt(args[0]);
    Percolation percolation = new Percolation(N);

    percolation.open(1, 1);
    percolation.open(2, 1);
    percolation.open(3, 1);
    percolation.open(4, 1);
    percolation.open(5, 1);
    StdOut.println(percolation.percolates());

    }
    }



    ..
    public class PercolationStats {
    private int T;
    private double[] result;

    public PercolationStats(int N, int T)
    {
    if (N <= 0 || T <= 0)
    throw new IllegalArgumentException("N and T must be greater than 0");


    this.T = T;

    Percolation percolation;
    result = new double[T];

    for (int t = 0; t < T; t++)
    {
    percolation = new Percolation(N);
    int openSites = 0;

    while (!percolation.percolates())
    {
    int i = StdRandom.uniform(N) + 1;
    int j = StdRandom.uniform(N) + 1;

    if (!percolation.isOpen(i, j))
    {
    percolation.open(i, j);
    openSites++;
    }
    }

    result[t] = (double) openSites / (N * N);
    }
    }

    public double mean()
    {
    return StdStats.mean(result);
    }

    public double stddev()
    {
    if (T > 1) return StdStats.stddev(result);
    else return Double.NaN;
    }

    public double confidenceLo()
    {
    return mean() - 1.96 * Math.sqrt(StdStats.var(result)) / Math.sqrt(T);
    }

    public double confidenceHi()
    {
    return mean() + 1.96 * Math.sqrt(StdStats.var(result)) / Math.sqrt(T);
    }

    public static void main(String[] args)
    {
    int N = Integer.parseInt(args[0]);
    int T = Integer.parseInt(args[1]);

    PercolationStats percolationStats = new PercolationStats(N, T);

    StdOut.printf("%-23s = %f\n", "mean", percolationStats.mean());
    StdOut.printf("%-23s = %f\n", "stddev", percolationStats.stddev());
    StdOut.printf("%-23s = %f, %f\n", "95% confidence interval",
    percolationStats.confidenceLo(),
    percolationStats.confidenceHi());
    }

    }

    ..


    import java.awt.Font;

    public class PercolationVisualizer {


    // draw N-by-N percolation system
    public static void draw(Percolation perc, int N) {
    StdDraw.clear();
    StdDraw.setPenColor(StdDraw.BLACK);
    StdDraw.setXscale(0, N);
    StdDraw.setYscale(0, N);
    StdDraw.filledSquare(N/2.0, N/2.0, N/2.0);


    // draw N-by-N grid
    int opened = 0;
    for (int row = 1; row <= N; row++) {
    for (int col = 1; col <= N; col++) {
    if (perc.isFull(row, col)) {
    StdDraw.setPenColor(StdDraw.BOOK_LIGHT_BLUE);
    opened++;
    }
    else if (perc.isOpen(row, col)) {
    StdDraw.setPenColor(StdDraw.WHITE);
    opened++;
    }
    else
    StdDraw.setPenColor(StdDraw.BLACK);
    StdDraw.filledSquare(col - 0.5, N - row + 0.5, 0.45);
    }
    }


    // write status text
    StdDraw.setFont(new Font("SansSerif", Font.PLAIN, 12));
    StdDraw.setPenColor(StdDraw.BLACK);
    StdDraw.text(.25*N, -N*.025, opened + " open sites");
    if (perc.percolates()) StdDraw.text(.75*N, -N*.025, "percolates");
    else StdDraw.text(.75*N, -N*.025, "does not percolate");


    }


    public static void main(String[] args) {
    In in = new In(args[0]); // input file
    int N = in.readInt(); // N-by-N percolation system


    // repeatedly read in sites to open and draw resulting system
    Percolation perc = new Percolation(N);
    draw(perc, N);
    while (!in.isEmpty()) {
    StdDraw.show(0); // turn on animation mode
    int i = in.readInt();
    int j = in.readInt();
    perc.open(i, j);
    draw(perc, N);
    StdDraw.show(50); // pause for 100 miliseconds
    }
    }
    }



    البته برای اجرای این برنامه به کتابخانه StdLib نیاز خواهید داشت که به همراه فایلهای ورودی براتون میزارم :
    http://uplod.ir/7m2sgvb3p4jg/percolation.rar.htm

  30. #70

    نقل قول: مجموعه برنامه‌های نوشته شده به زبان جاوا

    سلام
    همین برنامه رو اگه بخوایم تعداد فایل های هر فولدر رو برگردونه و ماکزیمم تعداد فایل های تمامی فولدر ها رو برگردونه چطور باید تغییر بدیم

  31. #71
    کاربر دائمی آواتار vahid-p
    تاریخ عضویت
    آذر 1391
    محل زندگی
    تهران
    پست
    1,140

    نقل قول: مجموعه برنامه‌های نوشته شده به زبان جاوا

    نقل قول نوشته شده توسط mohadese20 مشاهده تاپیک
    سلام
    همین برنامه رو اگه بخوایم تعداد فایل های هر فولدر رو برگردونه و ماکزیمم تعداد فایل های تمامی فولدر ها رو برگردونه چطور باید تغییر بدیم
    در همان تاپیک جواب داده شد : https://barnamenevis.org/showthread.p...=1#post2059782
    تا جای ممکن اینجا سوال پرسیده نشه و فقط کد صحیح و کاربردی قرار بگیره.

  32. #72

    نقل قول: مجموعه برنامه‌های نوشته شده به زبان جاوا

    خیلی سادس اولین کدی که نوشتم معذرت :مفهوم ارث بری


    package inheritance;
    import inheritance.b;
    import inheritance.inherittest;
    public class inherittestusing {
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    inherittest superob= new inherittest();
    b subob=new b();
    superob.i=10;
    superob.j=20;
    superob.showij();
    System.out.println();
    subob.i = 7;
    subob.j = 8;
    subob.k = 9;
    System.out.println("Contents of subOb :");
    subob.showij();
    subob.showk();
    System.out.println();
    System.out.print("sum:");
    subob.sum();
    }
    }









    package
    inheritance;

    public
    class inherittest {

    inti,j;

    void
    showij(){

    System.
    out.println("inherittest.showij"+i+" "+j);

    }

    }

    class
    b extends inherittest{

    intk;

    void showk(){


    System.
    out.println("b.showk()"+k);

    }

    void sum (){

    System.
    out.println("sum:"+(i+j+k));

    }

    }

    آخرین ویرایش به وسیله dasssnj : پنج شنبه 30 مرداد 1393 در 07:55 صبح

  33. #73
    مدیر بخش آواتار dasssnj
    تاریخ عضویت
    مرداد 1391
    محل زندگی
    Esfahan
    پست
    1,022

    نقل قول: مجموعه برنامه‌های نوشته شده به زبان جاوا

    سلام دوستان این تاپیک به اعلان ها اضافه شد .
    از دوستان تقاضای همکاری دارم .


    برای تشکر کافی است دکمه ی تشکر را بفشارید . از نوشتن کامنت زیر پست های مفید خودداری کنید .

    Write Once, Run Anywhere

  34. #74
    مدیر بخش آواتار dasssnj
    تاریخ عضویت
    مرداد 1391
    محل زندگی
    Esfahan
    پست
    1,022

    نقل قول: مجموعه برنامه‌ های نوشته شده به زبان جاوا

    اینم یه color chooser ساده.

    color.png

    MyColorChooser.zip

    Write Once, Run Anywhere

  35. #75
    مدیر بخش آواتار dasssnj
    تاریخ عضویت
    مرداد 1391
    محل زندگی
    Esfahan
    پست
    1,022

    نقل قول: مجموعه برنامه‌ های نوشته شده به زبان جاوا

    پیدا کردن عدد رندم بین دو عدد :

    public static int rand(int a, int b) {
    if (a == b)
    return -1;
    else if (a > b)
    return -1;
    else if (b - a == 1)
    return -1;
    Random r = new Random();
    int y = r.nextInt(b);
    if (y < a)
    y += (a - y) + r.nextInt(b - a);
    if (y != a)
    return y;
    else
    return y + 1 + r.nextInt((b - a) - 1);
    }


    البته اعداد پارامتر نباید مساوی باشند. دومی نباید کوچکتر از اولی باشه و تفاضلشون باید بیشتر از 1 باشه وگر نه 1- برمی گردونه.

    Write Once, Run Anywhere

  36. #76
    مدیر بخش آواتار dasssnj
    تاریخ عضویت
    مرداد 1391
    محل زندگی
    Esfahan
    پست
    1,022

    نقل قول: مجموعه برنامه‌ های نوشته شده به زبان جاوا

    پیدا کردن بزرگترین عدد بین هر چندتا عدد که دلتون بخواد !!!!!


    public static int max(int... i){
    int max;
    int c = i[0];
    max=c;
    for (int k = 1; k < i.length; k++){
    if(i[k]>max)
    max=i[k];
    }
    return max;
    }



    مثال :
    int i = max(2,-5,1000,50,60,600);
    int j = max(1,8,4,54,87,468,354,156,357,68,354,68,34,69);

    Write Once, Run Anywhere

  37. #77
    مدیر بخش آواتار dasssnj
    تاریخ عضویت
    مرداد 1391
    محل زندگی
    Esfahan
    پست
    1,022

    نقل قول: مجموعه برنامه‌ های نوشته شده به زبان جاوا

    تبدیل string به int با الگوریتم خودم.

    public static int strToint(String e) throws NumberFormatException {
    if (e == null || e.length() < 1 || e.equals("")) //string moshkel dare
    throw new NumberFormatException("String : " + e + " is corrupt !");

    char[] ch = e.toCharArray();
    boolean negative = false; //manfi
    int len = ch.length; // toole adad
    int[] i = new int[len]; //araye adad
    short j, k; // adad halghe ha
    int result = 0; //javab
    if (ch[0] == '-') { //manfi
    if (e.length() == 1)
    throw new NumberFormatException("String : " + e + " is corrupt !");

    negative = true; // manfi mishe
    ch = e.substring(1).toCharArray();
    len = ch.length;
    } else if (ch[0] == '+') { //mosbat
    if (e.length() == 1)
    throw new NumberFormatException("String : " + e + " is corrupt !");
    ch = e.substring(1).toCharArray();
    len = ch.length;
    }

    for (j = 0; j < len; j++) {
    if (ch[j] < '0' || ch[j] > '9') // bayad beine 0 ta 9 bashe
    throw new NumberFormatException("String : " + e + " Not a Number !");
    i[j] = ch[j] - 48; // az char ke adad bashe 48 ta kam koni mishe int
    for (k = 0; k < len - (j + 1); k++)
    i[j] *= 10; //sefr ha ra mizaram ba zarb dar 10

    result += i[j]; // hala jam mikonam
    if (result < 0) //aslan nabayad manfi bashe
    {
    if (result == -2147483648 && negative) {//No problem (-|0)
    // irad az man nist . system manfi bar migardoone . baray hamin in shart lazeme
    } else //adad bozorg tar az int
    {
    throw new NumberFormatException("Number : " + e + " is out of range for int! ");
    }
    }
    }
    return negative ? -result : result; // manfi bashe ya mosbat
    }

    Write Once, Run Anywhere

  38. #78
    کاربر دائمی آواتار ahmad.mo74
    تاریخ عضویت
    مرداد 1393
    محل زندگی
    تهران
    پست
    437

    نقل قول: مجموعه برنامه‌ های نوشته شده به زبان جاوا

    سلام،

    توی این تاپیک کمتر دیدم دوستان از مباحث multi threading و ... مطلب قرار بدن.

    یکی از موضوعاتی که به تازگی اهمیت پیدا کرده و به شدت تو افزایش سرعت اجرای تسک ها تاثیر گذاره، تقسیم کردن تسک های بزرگ به چندین تسک کوچیک و اجرای همزمان اون هاست.
    پیکیج java.util.concurrent یک API کاملیه که مخصوصا برای مدیریت بهتر concurrency ، بهره گیری کامل از توان سیستم برای انجام تسک ها و افزایش نظم و سرعت در اجرای برنامه های multi thread فراهم شده.
    واقعا حیفه که یه نگاهی به این پکیج و کلاس هاش انداخته نشه...

    برای مثال محاسبه فاکتوریل یک عدد نسبتا بزرگ میتونه کاملا اهمیت تجزیه تسک ها رو نشون بده هر چند مثال کاربردی نیست اما میتونید با دیدن این مثال برای موارد دیگه هم ایده بگیرید.

    فرض کنید میخوایم فاکتوریل عدد 100 رو حساب کنیم.

    یعنی :


    1 * 2 * 3 * ... * 100


    اگر بخوایم محاسبه این فاکتوریل رو به محاسبات کوچیک تر تقسیم کنیم :


    1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 * 12 * 13 * 14 * 15 * 16 * 17 * 18 * 19 * 20 * 21 * 22 * 23 * 24 * 25



    26 * 27 * 28 * 29 * 30 * 31 * 32 * 33 * 34 * 35 * 36 * 37 * 38 * 39 * 40 * 41 * 42 * 43 * 44 * 45 * 46 * 47 * 48 * 49 * 50



    51 * 52 * 53 * 54 * 55 * 56 * 57 * 58 * 59 * 60 * 61 * 62 * 63 * 64 * 65 * 66 * 67 * 68 * 69 * 70 * 71 * 72 * 73 * 74 * 75



    76 * 77 * 78 * 79 * 80 * 81 * 82 * 83 * 84 * 85 * 86 * 87 * 88 * 89 * 90 * 91 * 92 * 93 * 94 * 95 * 96 * 97 * 98 * 99 * 100


    حالا با محاسبه همه دنباله ها به صورت موازی داریم :


    15511210043330985984000000



    1960781468160819415703172080467968000000



    815712000579973729423615859451974909952000000



    3761767332187389431968739190317715670695936000000


    در آخر با ضرب همه نتیجه ها فاکتوریل عدد 100 رو حساب میکنیم :


    93326215443944152681699238856266700490715968264381 62146859296389521759999322991560894146397615651828 62536979208272237582511852109168640000000000000000 00000000



    FactorialCalculator :


    package com.sample.factorial;


    import java.math.BigInteger;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.ForkJoinPool;


    /**
    * @author avb
    */
    public class FactorialCalculator {


    private static final int AVAILABLE_PROCESSORS = Runtime.getRuntime().availableProcessors();
    private static final int OUTER_CHUNK_SIZE = AVAILABLE_PROCESSORS * AVAILABLE_PROCESSORS * 2;
    private static final int INNER_CHUNK_SIZE = AVAILABLE_PROCESSORS;


    private boolean parallel;


    public FactorialCalculator parallel() {
    parallel = true;
    return this;
    }


    public FactorialCalculator nonParallel() {
    parallel = false;
    return this;
    }


    public boolean isParallel() {
    return parallel;
    }


    public BigInteger calculate(int n) {
    if (n < 0) {
    throw new IllegalArgumentException();
    }
    if (n == 0) {
    return BigInteger.ONE;
    }
    Sequence sequence = new Sequence(1, n);
    if (!parallel) {
    return sequence.multiplySequence();
    }
    List<BigInteger> elements = parallelCalculate(splitSequence(sequence.getElemen ts(), OUTER_CHUNK_SIZE));
    while (elements.size() > INNER_CHUNK_SIZE) {
    elements = parallelCalculate(splitSequence(elements, INNER_CHUNK_SIZE));
    }
    return new Sequence(elements).multiplySequence();
    }


    private List<Sequence> splitSequence(List<BigInteger> elements, int chunkSize) {
    int index = 0, targetIndex, elementsSize = elements.size();
    List<Sequence> subSequences = new ArrayList<>();
    while (index < elementsSize) {
    targetIndex = Math.min(index + chunkSize, elementsSize);
    subSequences.add(new Sequence(elements.subList(index, targetIndex)));
    index = targetIndex;
    }
    return subSequences;
    }


    private List<BigInteger> parallelCalculate(List<Sequence> sequences) {
    CountDownLatch latch = new CountDownLatch(sequences.size());
    List<BigInteger> results = Collections.synchronizedList(new ArrayList<>());
    ForkJoinPool pool = new ForkJoinPool(AVAILABLE_PROCESSORS);
    sequences.parallelStream().forEach(sequence -> pool.submit(() -> {
    results.add(sequence.multiplySequence());
    latch.countDown();
    }));
    pool.shutdown();
    try {
    latch.await();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    return results;
    }


    public static class Sequence {


    private final List<BigInteger> elements;


    public Sequence(List<BigInteger> elements) {
    this.elements = elements;
    }


    public Sequence(int from, int to) {
    if (to < from) {
    throw new IllegalArgumentException();
    }
    elements = new ArrayList<>();
    for (; from <= to; from++) {
    elements.add(BigInteger.valueOf(from));
    }
    }


    public List<BigInteger> getElements() {
    return elements;
    }


    public BigInteger multiplySequence() {
    BigInteger result = BigInteger.ONE;
    for (BigInteger element : elements) {
    result = result.multiply(element);
    }
    return result;
    }


    }


    }


    Test :


    package com.sample.factorial;


    /**
    * @author avb
    */
    public class Test {


    public static void main(String[] args) throws InterruptedException {
    final FactorialCalculator factorialCalculator = new FactorialCalculator();
    for (int i = 0; i < 10; i++) {
    System.out.println("test " + (i + 1) + " : ");
    long l = System.currentTimeMillis();
    factorialCalculator.parallel().calculate(1000000);
    System.out.println("Execution time : " + (System.currentTimeMillis() - l) + " ms");
    System.out.println("----------------------------");
    }
    }


    }


    برای مقایسه محاسبه فاکتوریل عدد 1000000 به صورت معمولی حدودا 5-3 دقیقه طول میکشه اما به صورت parallel حدودا 7 ثانیه روی یه پردازنده 8 هسته طول میکشه !!! به شدت که میگم برای اینه :))

    دانلود src
    آخرین ویرایش به وسیله ahmad.mo74 : پنج شنبه 11 دی 1393 در 11:23 صبح

  39. #79
    کاربر دائمی آواتار Raminab
    تاریخ عضویت
    مرداد 1392
    محل زندگی
    همدان
    پست
    125

    نقل قول: مجموعه برنامه‌ های نوشته شده به زبان جاوا

    یه notepad ساده با عملیات open,save,copy,cut,past

    http://s5.picofile.com/file/8163858584/MYNOtePad.rar.html

  40. #80
    کاربر دائمی آواتار kingtak
    تاریخ عضویت
    خرداد 1388
    محل زندگی
    اینترنت
    پست
    231

    نقل قول: مجموعه برنامه‌ های نوشته شده به زبان جاوا

    این هم حل مساله ۸ وزیر با الگوریتم ژنتیکه که برای پروژه هوش مصنوعی در دانشگاه نوشته بودم.برنامه درست کار میکنه.ولی اگه کسی خواست مینونه بهترش بکنه.
    https://github.com/kingtak66/N-Queen

صفحه 2 از 3 اولاول 123 آخرآخر

تاپیک های مشابه

  1. مجموعه برنامه های نوشته شده به زبان C و ++C
    نوشته شده توسط Nima_NF در بخش برنامه نویسی با زبان C و ++C
    پاسخ: 319
    آخرین پست: جمعه 11 اسفند 1396, 14:11 عصر
  2. مبتدی: برای اجرای برنامه های نوشته شده به زبان جاوا با استفاده از notpad++ چه باید کرد؟
    نوشته شده توسط helia64 در بخش برنامه‌نویسی جاوا
    پاسخ: 2
    آخرین پست: سه شنبه 19 شهریور 1392, 15:33 عصر
  3. آموزش: مجموعه برنامه‌های نوشته شده به زبان جاوا
    نوشته شده توسط m.a.d1992 در بخش Java SE : نگارش استاندارد جاوا
    پاسخ: 0
    آخرین پست: یک شنبه 02 تیر 1392, 18:56 عصر
  4. گفتگو: efficiency - بازدهی زمان اجرا در برنامه های نوشته شده به زبان جاوا + یک مثال
    نوشته شده توسط jlover در بخش برنامه‌نویسی جاوا
    پاسخ: 10
    آخرین پست: شنبه 22 خرداد 1389, 09:57 صبح
  5. اتصال ترازوی دیجیتال به برنامه های نوشته شده به زبان دلفی
    نوشته شده توسط DlphIran در بخش برنامه نویسی در Delphi
    پاسخ: 4
    آخرین پست: شنبه 06 خرداد 1385, 07:30 صبح

برچسب های این تاپیک

قوانین ایجاد تاپیک در تالار

  • شما نمی توانید تاپیک جدید ایجاد کنید
  • شما نمی توانید به تاپیک ها پاسخ دهید
  • شما نمی توانید ضمیمه ارسال کنید
  • شما نمی توانید پاسخ هایتان را ویرایش کنید
  •