By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,798 Members | 1,349 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,798 IT Pros & Developers. It's quick & easy.

Need some help getting started with my assigment...

P: 6
Part one of mye assignment is:

The method must be declared public static, accept a string as a parameter, and return a string. The method receives a name on the form "Firstname [Middlename(s)] Lastname" and should return the same name on the form "Lastname, FirstName Middlename(s)". The method must additionally correct lowercase/uppercase errors and return names with only the first letter of each part in uppercase.

Example: The input "Per petTer Pettersen" must be returned as "Pettersen, Per Petter".
I've started with this, but don't know if it's the right start at all..
I'm VERY new to Java..

Expand|Select|Wrap|Line Numbers
  1.  
  2. public class NameUtil {
  3.  
  4.     public static String normalize(String String){
  5.  
  6.  
  7.         return String;
  8.     }
  9.  
  10.  
  11. }
  12.  
Jan 31 '07 #1
Share this Question
Share on Google+
8 Replies


P: 6
Part two of my assignment

Person class
Create the class Person in package oving2 with the following fields:
public String name
The name field holds the person's name.
public Person mother
The mother field holds a reference to the person's mother.
public Person father
The father field holds a reference to the person's father.
public Person[] children
The children field holds a reference to an array of the person's children.

This one is ok I think...
Expand|Select|Wrap|Line Numbers
  1. public class Person {
  2.     public String name;
  3.     public Person mother;
  4.     public Person father;
  5.     public Person[]children;
Part 2.2: The Royal Family
Implement the following method in the Person class:
public static Person createRoyalFamily()
This method should create an object graph corresponding to the royal family descending from King Harald and Queen Sonja.

The royal family consists of Harald, Sonja, their children Haakon and Martha. Haakon has two children with Mette-Marit named Ingrid Alexandra and Sverre Magnus, and Martha has two children with Ari named Maud Angelica and Leah Isadora.

The createRoyalFamily method must create Person objects corresponding to all member of the Royal family, and correctly set their name, mother, father, and children fields. Finally, the Person object corresponding to King Harald must be returned.

Note that the createRoyalFamily method must be declared static, since it will be called without any reference to any Person object.

Part 2.3: Family relations
Implement the following methods in the Person class:
public boolean isMotherOf(Person)
a method returns whether this Person is the mother of the Person given as parameter.
public boolean isFatherOf(Person)
a method returns whether this Person is the father of the Person given as parameter.
public boolean isSiblingOf(Person)
a method returns whether this Person is a sibling of the Person given as parameter.
public boolean isHalfSiblingOf(Person)
a method returns whether this Person is a half sibling of the Person given as parameter. A person is another person's half sibling if they have the same mother or father but not both.

Would be nice with som tips and hints on the different parts of this one..
Jan 31 '07 #2

P: 6
Par three:

Create a class named Card with the following fields (variables) and method:
public String suit

holds a String of either "S", "H", "D", or "C" (Spades, Hearts, Diamonds, or Clubs).

public int face

holds an integer number in the range 1-13.
public String toString()


a method that takes no parameters and returns a string consisting of the suit and face of the card, eg. "S2", "H3", or "C5". This method is convenient for printing out a card, e.g. using System.out.println(card).

I've started with this:
Expand|Select|Wrap|Line Numbers
  1. public class Card {
  2.     public String suit;
  3.         String suit1 = new String ("S");
  4.         String suit2 = new String ("H");
  5.         String suit3 = new String ("D");
  6.         String suit4 = new String ("C");
  7.     public int face;
  8.  
  9.  
  10.     public String toString();
  11. }
  12.  
  13.  

Part 3.2: A deck of cards

Create a class named CardDeck with the following field and methods:
public Card[] cards
an array that holds references to 52 different cards
public void buildDeck()
a method that builds a correct deck of cards (in the cards array) with the following order: 1-13 of spades, 1-13 of hearts, 1-13 of diamonds, and finally 1-13 of clubs.
public Card getCard(int)
a method that takes an int as a parameter and returns the card at the given index in the card array.

Part 3.3: Shuffling and dealing of cards
Implement the following methods in the CardDeck class:
public void shuffle()
a method that shuffles the cards in the cards array, returns nothing.
public Card[] deal(int)
a method that deals a number of cards from the top of the deck (takes an int as a parameter and returns an array of cards). If called several times the method continues to deal from the same deck of cards until the deck is empty. Dealing should be implemented by returning elements from the array, and you need to keep track of what cards that have been dealt by the use of a counter. If there is not enough cards in the deck, or if the deck is empty, the method should return an empty array of cards.
public int getCardCount()
a method that returns the number of cards left in the deck. E.g. if 5 cards are dealt, it should return 47.

As I've said in post 1, I really am new to Java, and I need all the help I can get...
Jan 31 '07 #3

Ganon11
Expert 2.5K+
P: 3,652
What work have you done so far? What code do you have? If you have no code, what are your ideas about the problem?
Jan 31 '07 #4

P: 6
What work have you done so far? What code do you have? If you have no code, what are your ideas about the problem?
Well, that's the problem... I don't have more than the small fractions of code I've put in my resent posts..
I really don't know where to start...
I could need som pointers to where I should start..
Jan 31 '07 #5

10K+
P: 13,264
Part one of mye assignment is:

The method must be declared public static, accept a string as a parameter, and return a string. The method receives a name on the form "Firstname [Middlename(s)] Lastname" and should return the same name on the form "Lastname, FirstName Middlename(s)". The method must additionally correct lowercase/uppercase errors and return names with only the first letter of each part in uppercase.

Example: The input "Per petTer Pettersen" must be returned as "Pettersen, Per Petter".
I've started with this, but don't know if it's the right start at all..
I'm VERY new to Java..

Expand|Select|Wrap|Line Numbers
  1.  
  2. public class NameUtil {
  3.  
  4.     public static String normalize(String String){
  5.  
  6.  
  7.         return String;
  8.     }
  9.  
  10.  
  11. }
  12.  
One part at a time.

Start by splitting the name into an array using the string.split method e.g

Expand|Select|Wrap|Line Numbers
  1.  String s = "r0 35 19  8x"; 
  2. String[] name = s.split(" ");
  3.  
Splits s on " "(space) into an array.

You should now be able to see how to complete it.
Feb 1 '07 #6

P: 6
One part at a time.

Start by splitting the name into an array using the string.split method e.g

Expand|Select|Wrap|Line Numbers
  1.  String s = "r0 35 19  8x"; 
  2. String[] name = s.split(" ");
  3.  
Splits s on " "(space) into an array.

You should now be able to see how to complete it.
I've done this now:
Expand|Select|Wrap|Line Numbers
  1. public class NameUtil {
  2.  
  3.     public static String normalize(String string){
  4.  
  5.         String[] name = string.split(" ");
  6.         String capital = name[name.length-1].substring(0, 1).toUpperCase();
  7.         String rest =name[name.length-1].substring(1).toLowerCase();
  8.         String done = "";
  9.         for(int i=0; i<name.length-1; i++){
  10.             String fo = name[i].substring(0, 1).toUpperCase();
  11.             String re =name[i].substring(1).toLowerCase();
  12.             done = done + " " + fo + re;
  13.         }
  14.         done = capital + rest + "," + done;
  15.         return done;
  16.     }
  17.  
  18.  
  19. }
  20.  
Feb 2 '07 #7

10K+
P: 13,264
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. public class Test2 {
  4.  public static void main(String[] args) {
  5.  
  6.   System.out.println(normalize(\"Per petTer Pettersen\"));
  7.  }
  8.  public static String normalize(String string) {
  9.  
  10.   String[] name = string.split(\" \"); 
  11.  
  12.   //proper case everything
  13.  
  14.   for(int i = 0; i < name.length;i++) {
  15.    name[i] = name[i].toLowerCase();
  16.    name[i] = upperFirstChar(name[i]);
  17.   }
  18.  
  19.   //now get the first name and lastname.
  20.  
  21.   String firstName = name[0];
  22.  
  23.   String lastName = name[name.length - 1];
  24.  
  25.   String middleNames = \"\";
  26.   for(int i = 1; i < (name.length - 1); i++) {
  27.    middleNames = middleNames + \" \"+ name[i];
  28.   }
  29.   return lastName + \" \" + firstName + \" \" + middleNames;
  30.  
  31.  
  32.  
  33.  }
  34.  public static String upperFirstChar(String word) {
  35.   char c = word.charAt(0);
  36.   String s = \"\"+c;
  37.   s = s.toUpperCase();
  38.   s = s + word.substring(1, word.length());
  39.   return s;
  40.  }
  41.  
  42. }
  43.  
  44.  


P.S at this rate you won\'t finish the rest of the assignments.
Feb 2 '07 #8

P: 6
I got some help from a classmate this weekend, so I got my assignment done in due time. Thank you for helping though
Feb 5 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.