Dhanuka Nilanjana

Active member
  • Aug 9, 2009
    700
    89
    28
    Dompe,Sri Lanka
    මචන්ලා මට java programme එකක් හදන්න වෙලා තියෙනවා මේකෙන් වෙන්න ඕන දේ සරලව කිව්වොත් කාසියක් උඩ දැම්මම 1000 වාරයක් සිරස ලැබෙන්න කී වාරයක් කාසිය උඩ දාන්න ඕනිද​? කියලා තමයි ගණනය වෙන්න ඕනි. පහල තියෙන්නෙ මේකෙ ප්‍රශ්නය​

    Write a simulator programme that flip a coin:
    1. One thousand time then print out how many time you got tail and how many time you got head.

    2. Continuously flipping coin until you find 1000 head count then print out how many time the coin has been flipped.


    මේක හදාගන්න කවුරුහරි help එකක් දෙන්න​.


    :(
     

    amilabanuka

    Well-known member
  • Sep 30, 2006
    7,291
    878
    113
    Thama math hoyanooo....
    without UI
    generate a random number in anyway you like. if the scale is low make is bit larger. then take the modulo 2 so you'll get either 1 or 0. nominate those as you like. then you have coin flipping.
    then you can loop it and get the results
     

    tharindudoo

    Well-known member
  • Mar 12, 2007
    10,906
    761
    113
    ..ජනේලේ ගාව....
    2nd part ekedi loop ekak athule random number generate function 1k danna dala a ena number eka %2 aragena 1 ena ava head vidiyatath 0 ena ava tail vidiyatath ganna,
    variable (x)1k dala increment karanna rand number eka generate vena hama velaavakama,thava variable 2k dala 1 avoth eka variable 1k increment karanna 0 avoth anith variable eka increment karann,1 kiyanne head ne,1 avahama incremen vena variable eka == 1000ta print karanna x ge count eka a kiyanne head 1000k vatenna generate vena numbers gaana x variable eken print karanava,

    me logic eka,
    code 1k uba gahapan
     

    MihiCherub

    Well-known member
  • Sep 14, 2009
    18,849
    1
    9,594
    113
    Gampaha
    import java.util.Random;
    class flipaCoin{

    static int h1, t1;

    public static void main (String [] mihi){

    Random flippinCoin = new Random();

    for (int i = 0; i < 1000; i++) {
    int flip = flippinCoin.nextInt(2) + 1;

    if(flip==1){
    System.out.println("Head");
    h1++;
    }else{
    System.out.println("Tail");
    t1++;
    }
    }
    System.out.println("Head = "+h1+"\nTail = "+t1);
    }
    }
    ------------------------------------------------------------

    import java.util.Random;
    class flipaCoin{

    static int h1, t1;

    public static void main (String [] mihi){

    Random flippinCoin = new Random();

    while(true) {

    int flip = flippinCoin.nextInt(2) + 1;

    if(flip==1){
    System.out.println("Head");
    h1++;
    }else{
    System.out.println("Tail");
    t1++;
    }
    if(h1==1000){
    break;
    }
    }
    int total = t1+h1;
    System.out.println("Head = "+h1+"\nTail = "+t1+"\nTimes = "+total);
    }
    }
    :D:D මචන් බලපන් හරිද කියල. :D:D
     
    Last edited:

    MihiCherub

    Well-known member
  • Sep 14, 2009
    18,849
    1
    9,594
    113
    Gampaha
    Badu wada kollo me magule lechata Do while loop ekenlu oni uge achchita hal garanna util random use karannepalu
    ඉතිං බන් Random class එක නැතුව කොහොමද අහඹු සිද්ධි ගන්නෙ. එහෙනම් Thread scheduler එක්ක පුලුවන් Synchronized method වලින්. ඒත් do-while loop එකෙන් කරන්න පුලුවන් විදියක් තිබුනත් මට නම් මේ වෙලාවෙ මතක් වෙන්නෙ නෑ මචන්. :sorry::sorry:
     

    Shamil Max

    Well-known member
  • May 18, 2008
    7,379
    2,206
    113
    Boralesgamuwa
    243hmyv.gif
     

    MihiCherub

    Well-known member
  • Sep 14, 2009
    18,849
    1
    9,594
    113
    Gampaha
    Badu wada kollo me magule lechata Do while loop ekenlu oni uge achchita hal garanna util random use karannepalu
    දැන්වත් හරිද බලපන්... කෝ බන් උබට Reply කරන්න පට්ට වෙලාවක් යනවනෙ. :frown::frown: අපේ අමාරුවට උබට කියල දෙනව වගේ. :angry::angry::angry: උබ අර දුන්නයි කියපු රෙප් ආවෙත් නෑ. කොහොමත් රෙප් බලාගෙන නෙමේ කලේ.. :oo::oo:

    class flipaCoin{

    static int head, tail;

    public static void main(String[] MihiCherub) {

    int result = 0 , count = 0;

    do{
    result = (int) (Math.random() *2 + 1);

    if(result==1){
    System.out.println("Head");
    head++;
    }

    else if(result==2){
    System.out.println(" Tail");
    tail++;
    }
    else{
    System.out.println("foul");
    }
    count++;
    }
    while(count < 1000);
    System.out.println("\nTotal head "+ head + "\nTotal tail "+tail);
    }

    }

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

    class flipaCoin{

    static int head, tail;

    public static void main(String[] MihiCherub) {

    int result = 0 , count = 0;

    do{
    result = (int) (Math.random() *2 + 1);

    if(result==1){
    System.out.println("Head");
    head++;
    }

    else if(result==2){
    System.out.println(" Tail");
    tail++;
    }
    else{
    System.out.println("foul");
    }
    count++;
    }
    while( head < 1000);
    System.out.println("\nTotal head "+ head + "\nTotal tail "+tail);
    System.out.println("Total "+count);
    }

    }
     

    Dhanuka Nilanjana

    Active member
  • Aug 9, 2009
    700
    89
    28
    Dompe,Sri Lanka
    1. Write a program that accepts ID, Name, E-mail address and Contact No. of all the students in a class and saves them in an array. Ask the user for the size of the class and the student details.


    Meka iwara karanna help ekak dendoooooooooooo
    pahala tiyenne mama liwwa programme eka


    .....................................................................................................................
    import javax.swing.*;

    public class ClassReg{

    public static void main(String[] args){

    String classsize=JOptionPane.showInputDialog("Enter the number of students in the class?");

    int students=Integer.parseInt(classsize);

    int[] stid=new int[students];
    String[] stname=new String[students];
    String[] stphone=new String[students];
    String[] stemail=new String[students];

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

    String studentid=JOptionPane.showInputDialog("Enter ID of the student "+ (i+1));

    int idno=Integer.parseInt(studentid);

    stid=idno;


    String studentname=JOptionPane.showInputDialog("Enter NAME of the student "+ (i+1));

    stname=studentname;


    String studentphone=JOptionPane.showInputDialog("Enter contact number of the student "+ (i+1));

    stphone=studentphone;

    String studentmail=JOptionPane.showInputDialog("Enter the Email address of the student?"+ (i+1));

    stemail=studentmail;


    system.out.println("student id is "+idno +", " +" student name is "+stname+""+" phoneno is"+stphone+","+"email address is"+stemail);


    }

    }

    }
     

    Dhanuka Nilanjana

    Active member
  • Aug 9, 2009
    700
    89
    28
    Dompe,Sri Lanka
    දැන්වත් හරිද බලපන්... කෝ බන් උබට Reply කරන්න පට්ට වෙලාවක් යනවනෙ. :frown::frown: අපේ අමාරුවට උබට කියල දෙනව වගේ. :angry::angry::angry: උබ අර දුන්නයි කියපු රෙප් ආවෙත් නෑ. කොහොමත් රෙප් බලාගෙන නෙමේ කලේ.. :oo::oo:



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

    rep dunna ban indapanko pennanna
     

    Dhanuka Nilanjana

    Active member
  • Aug 9, 2009
    700
    89
    28
    Dompe,Sri Lanka
    1. Write a program that accepts ID, Name, E-mail address and Contact No. of all the students in a class and saves them in an array. Ask the user for the size of the class and the student details.


    Meka iwara karanna help ekak dendoooooooooooo
    pahala tiyenne mama liwwa programme eka


    .....................................................................................................................
    import javax.swing.*;

    public class ClassReg{

    public static void main(String[] args){

    String classsize=JOptionPane.showInputDialog("Enter the number of students in the class?");

    int students=Integer.parseInt(classsize);

    int[] stid=new int[students];
    String[] stname=new String[students];
    String[] stphone=new String[students];
    String[] stemail=new String[students];

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

    String studentid=JOptionPane.showInputDialog("Enter ID of the student "+ (i+1));

    int idno=Integer.parseInt(studentid);

    stid=idno;


    String studentname=JOptionPane.showInputDialog("Enter NAME of the student "+ (i+1));

    stname=studentname;


    String studentphone=JOptionPane.showInputDialog("Enter contact number of the student "+ (i+1));

    stphone=studentphone;

    String studentmail=JOptionPane.showInputDialog("Enter the Email address of the student?"+ (i+1));

    stemail=studentmail;


    system.out.println("student id is "+idno +", " +" student name is "+stname+""+" phoneno is"+stphone+","+"email address is"+stemail);


    }

    }

    }




    .............................................
    mekata udaw karanna java wadakarayo asaranayata pihita wethwaaaaaa!!!!!!