Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: Is there any intelligent person in the Java programming? plz

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Is there any intelligent person in the Java programming? plz

    Please help me to solve this: (
    Only three questions....



    The questions given below are based on the lecture notes, and are intended to familiarize you with concepts of arrays, ArrayList and inheritance.


    1. Arrays
    Write a program that read in 16 values from the keyboard and store them into a two dimension array (myArray). The program then calculate the sum of every row elements and every column elements an put this sum into elements of two arrays (one for the rows – rowSum and the other for columns- colSum ) ,then the program print the three arrays.



    2. ArrayList class

    You are to write a simple names book program. A names book contains names of your friends. Store your names book as an ArrayList with entries contains strings that represent the names. Your program must tell you how many friends you have and which one of them has the longest name.


    3. Inheritance

    Design and draw the UML class diagram of a set of classes that define the employees of a Hospital, the classes are:
    HospitalPerson , Doctor and Nurse.
    Where HospitalPerson is a supper class, Doctor and Nurse are subclasses.

    Design your classes to satisfy the following requirements:
    1. Each HospitalPerson has a name and an idNumber.
    2. A Doctor has in addition a specialty attribute, while a Nurse has a
    maxDutyHours attribute.
    3. We want to have a special print (output information) as following:
    a. The name and idNumber of a HospitalPerson works for the Hospital.
    b. The specialty of a Doctor.
    c. The maxDutyHours of a Nurse.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Is there any intelligent person in the Java programming? plz

    We won't do your homework for you. Please show us what you have done so far and we'll help you with any problems you have.

  3. #3
    Junior Member
    Join Date
    May 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Is there any intelligent person in the Java programming? plz

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
     
    public class ArrayProgram {
     
    public static void main(String[] args)
    {
    final BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    // Get the 16 numbers from the user and store them into a one dimensional array
    int[] myArray = new int[16];
    for (int i = 0; i <myArray.length; i++) {
    System.out.print("Enter number: ");
    System.out.print(i + 1);
    System.out.println(" in one dimensional array:"); {
     
    myArray = Integer.valueOf(input.readLine()).intValue();
    } catch (IOException e) {
     
    e.printStackTrace();
    System.exit(1);
    } 
    }
    __________________________?

    is right?
    Last edited by helloworld922; May 14th, 2010 at 04:31 PM. Reason: Please use [code] tags please

  4. #4
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Is there any intelligent person in the Java programming? plz

    Quote Originally Posted by alaseier View Post
    is right?
    Which is the question that asks you to put 16 numbers into a one-dimensional array?

  5. #5
    Junior Member
    Join Date
    May 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Is there any intelligent person in the Java programming? plz

    Quote Originally Posted by dlorde View Post
    Which is the question that asks you to put 16 numbers into a one-dimensional array?
    Write a program that read in 16 values from the keyboard and store them into a two dimension array (myArray).

  6. #6
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Is there any intelligent person in the Java programming? plz

    Quote Originally Posted by alaseier View Post
    Write a program that read in 16 values from the keyboard and store them into a two dimension array (myArray).
    No, that's a question about a two dimensional array, not a one dimensional array. Notice the difference?

  7. #7
    Junior Member
    Join Date
    May 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Is there any intelligent person in the Java programming? plz

    how can solve that???

  8. #8
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    490
    Thanks
    8
    Thanked 46 Times in 44 Posts

    Default Re: Is there any intelligent person in the Java programming? plz

    Link --> The Java™ Tutorials

    db

  9. #9

    Default Re: Is there any intelligent person in the Java programming? plz

    int[][] TwoD = new int[50][50];

  10. #10
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Is there any intelligent person in the Java programming? plz

    In java, two dimensional arrays are just arrays of arrays. So it depends on how you want to store them into the array.

    So, say you wanted to store your data into a 4x4 array (makes sense since you have 16 numbers):

    int[][] data = new int[4][4];
    Scanner reader = new Scanner(System.in);
    for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            System.out.print("Enter a number to put into [" + i + "][" + j + "]: ");
            data[i][j] = reader.nextInt();
        }
    }

    Of course, a 1-d Array can be propogated easily into a 2-D array:

    int[] oneDimension = new int[16];
    int[][] twoDimensions = new int[1][];
    twoDimensions[0] = oneDimension; // now have a 1x16 array

  11. #11
    Junior Member
    Join Date
    May 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Is there any intelligent person in the Java programming? plz

    Quote Originally Posted by helloworld922 View Post
    In java, two dimensional arrays are just arrays of arrays. So it depends on how you want to store them into the array.

    So, say you wanted to store your data into a 4x4 array (makes sense since you have 16 numbers):

    int[][] data = new int[4][4];
    Scanner reader = new Scanner(System.in);
    for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            System.out.print("Enter a number to put into [" + i + "][" + j + "]: ");
            data[i][j] = reader.nextInt();
        }
    }

    Of course, a 1-d Array can be propogated easily into a 2-D array:

    int[] oneDimension = new int[16];
    int[][] twoDimensions = new int[1][];
    twoDimensions[0] = oneDimension; // now have a 1x16 array
    import java.util.Scanner;
    import java.util.ArrayList;
     public class Data
     {
    public static void main(String[] args)
    {
    int[][] data = new int[8][8];
    Scanner reader = new Scanner(System.in);
    for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < 8; j++)
        {
            System.out.print("Enter a number to put into [" + i + "][" + j + "]: ");
            data[i][j] = reader.nextInt();
        }
    }


    -------------------------------------------------------------------------------------------------
    Is rigth now?
    Last edited by helloworld922; May 18th, 2010 at 12:06 AM.

  12. #12
    Member
    Join Date
    Jan 2010
    Posts
    42
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Is there any intelligent person in the Java programming? plz

    sorry, i aint smart. cant help you. HAHA!

  13. #13
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Is there any intelligent person in the Java programming? plz

    Without knowing how it is you want it stored, there's no way for me to know if it's right or wrong. However, I will tell you that you are entering in 4 rows of data at 8 columns each, so that's 4 * 8 = 32 numbers, which may or may not be what you want since you said you wanted 16 numbers entered.

  14. #14
    Junior Member
    Join Date
    May 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Is there any intelligent person in the Java programming? plz

    import java.util.Scanner;
     
     
    public class TowDArray{
     public static void main(String[] args){
     
      int[][] myArray=new int[4][4];
      Scanner in=new Scanner(System.in);
      System.out.println("ENTER THE NUMBER");
     
      for(int row=0; row<myArray.length; row++)
      for(int col=0; col<[row].length; col++)
      myArray[row]=in.nextInt();
      myArray[col]=in.nextInt();
     
       for(int row=0; row<myArray.length; row++)
       for(int col=0; col<[row].length; col++)
       System.out.println(myArray[row][col]);
     
       int b=0;
       int[] rowSum=new int[4];
       for(int row=0; row<rowSum.length; row++)
       rowSum[row]=rowSum[row]+b;
       System.out.println("THE ROWS SUM :");
       System.out.println(rowSum[row]);
     
       int[] colSum=new int[4];
       for(int col=0; col<colSum.length; col++)
       colSum[col]=colSum[col]+b;
       System.out.println("THE COLUMNS SUM :");
       System.out.println(colSum[col]);
    }
    }
    --------------------------------

    tell me of now is rigth.
    Last edited by Json; May 24th, 2010 at 03:18 AM. Reason: Please use code tags

  15. #15
    Member
    Join Date
    May 2010
    Posts
    38
    Thanks
    1
    Thanked 8 Times in 7 Posts

    Default Re: Is there any intelligent person in the Java programming? plz

    Quote Originally Posted by alaseier View Post
    tell me of now is rigth.
    Are you even testing this? Just use System.out.println as debugging statements. Print the array and see if thats what you want. If you still can't get it, then read some of the Java tutorials, their actually kinda good

  16. #16
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Is there any intelligent person in the Java programming? plz

    Quote Originally Posted by alaseier View Post
    Please help me to solve this: (
    Only three questions....



    The questions given below are based on the lecture notes, and are intended to familiarize you with concepts of arrays, ArrayList and inheritance.


    1. Arrays
    Write a program that read in 16 values from the keyboard and store them into a two dimension array (myArray). The program then calculate the sum of every row elements and every column elements an put this sum into elements of two arrays (one for the rows – rowSum and the other for columns- colSum ) ,then the program print the three arrays.

    What kind of array? char, int, long, String, double, short, float, class, etc?

    int n ;
    System.out.println("Enter the number of rows. ");
    n = console.nextInt();
    System.out.println("Enter the number of columns. ");
    v = console.nextInt();

    dataType myArray = new dataType[n][v];

    Use the clone method to get others. To get sum of elements in rows:

    sum = 0;
    int col = 0;
    row = // row number;
    // this finds the sum of elements in row n;

    for (col = 0; col < myArray[row].length; col++)
    sum = sum + myArray[col][row];

    // sum of each individual row

    for (row = 0; row < myArray.length; row++)
    {
    sum = 0;
    for (col = 0; col < myArray[row].length; col++)
    sum = sum + myArray[row][col];
    // each final sum value is one element
    // add the sum values of each row to get array.

    // sum of each individual column

    int col2, row2, sum2;
    col2 = 0;
    row2 = 0;
    sum2 = 0;

    for (col 2= 0; col2< myArray[0].length; col2++)
    {
    sum 2= 0;
    for (row2 = 0; row2 < myArray.length; row2 ++)
    sum2 = sum2 + myArray[row2][col2];
    // each final sum value is an element;
    // add the sum values for all columns to get array.


    }





    2. ArrayList class

    You are to write a simple names book program. A names book contains names of your friends. Store your names book as an ArrayList with entries contains strings that represent the names. Your program must tell you how many friends you have and which one of them has the longest name.

    Use compareTo() perhaps.

    Could you use a fileReader or something to get them in? If so, throw a FileNotFoundException at the end of the declaration of main.


    3. Inheritance

    Design and draw the UML class diagram of a set of classes that define the employees of a Hospital, the classes are:
    HospitalPerson , Doctor and Nurse.
    Where HospitalPerson is a supper class, Doctor and Nurse are subclasses.

    Design your classes to satisfy the following requirements:
    1. Each HospitalPerson has a name and an idNumber.
    2. A Doctor has in addition a specialty attribute, while a Nurse has a
    maxDutyHours attribute.
    3. We want to have a special print (output information) as following:
    a. The name and idNumber of a HospitalPerson works for the Hospital.
    b. The specialty of a Doctor.
    c. The maxDutyHours of a Nurse.
    Sounds more like composition.

    Inheritance is an "is a" relationship.
    Composition is a "has a" relationship.

    Nurse and Doctor are inherited from HospitalPerson yes, as Doctor is a HospitalPerson and Nurse is a Hospital Person.

    Public class Doctor Extends HospitalPerson
    {

    public Doctor()
    {

    }

    }

    Also, if you're overriding a method, use method name of superclass like this.
    super.methodOfSuperClass(methodsParameters);

    It seems you could either have abstract methods, methods defined like so:

    public abstract String getName();
    public abstract String getIDNumber();

    in class HospitalPerson and define them in Doctor and Nurse.....
    or you could use composition and use a "has a relationship" for name and IDNumber.

Similar Threads

  1. Please help me with this Java programming
    By simply_stunning79 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 12th, 2010, 07:04 AM
  2. New person Just trying to read a file of ints
    By dubois.ford in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 7th, 2010, 10:47 PM
  3. Please help me with this Java programming
    By simply_stunning79 in forum Algorithms & Recursion
    Replies: 1
    Last Post: February 22nd, 2010, 06:48 PM
  4. Please help me with this Java programming
    By simply_stunning79 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 14th, 2010, 05:42 AM
  5. Replies: 2
    Last Post: November 3rd, 2009, 05:28 AM