Java Programming Help!!!

Discussion in 'School Work Help' started by dom88_rx7, Feb 2, 2010.

  1. dom88_rx7

    dom88_rx7 Well-Known Member

    Java Programming Help Use Textpad!!!

    A common memory matching game played by young children is to start with a deck of cards that contain identical pairs. For example, given a six cards in the deck, two might be labeled 1, two labeled 2, and two labeled 3. The cards are shuffled and placed face down on the table. A player then selects two cards that are face down, turns the face up, and if the cards match they are left face up. If the two cards do not match, they are returned to their original face down position. The game continues until all the cards are face up.

    Write a program that plays the memory matching game. Use sixteen card that are laid out in a 4x4 square that are labeled with pairs of numbers from 1 to 8. Your program should allow the player to specify the cards that she would like to select through a coordinate system.

    For example, in the following layout:

    1 2 3 4
    1 8 * * *
    2 * * * *
    3 * 8 * *
    4 * * * *

    All of the cards that are face down are indicated by a *. The pairs of 8 which are face up are at coordinates (1,1) and (2,3). To hide the cards that have been temporarily places face up, output a large number of newlines to force the old board off the screen.

    Hint: Use a 2D array for the arrangement of cards and another 2D array that indicates if a card is face up or down. Or, a more elegant solution is to create a single 2D array where each element is an object that stores both the card’s value and face. Write a function that “shuffles” the cards in the array.
    #1 dom88_rx7, Feb 2, 2010
    Last edited: Feb 2, 2010
  2. spider-man

    spider-man Well-Known Member

    I think the shuffling is like this. I don't really know the syntax that well.

    The code shows it running through every card, while switching places with another random card at a random place

    Random rgen = new Random(); // Random Object
    int[][] cards = new int[5][5]; // Enter the numbers. Not sure if that's right.

    for (int i=0; i<cards.length; i++) {
    for (int j=0; j<cards.length; j++) {
    int randomPosition = rgen.nextInt(cards.length);
    int randomPositionTwo = rgen.nextInt(cards.length);
    int temp = cards[j];
    cards[j] = cards[randomPosition][randomPositionTwo];
    cards[randomPosition][randomPositionTwo] = temp;

    By the way your diagram should be like 0 1 2 3.
    #2 spider-man, Feb 5, 2010
    Last edited: Mar 1, 2010
  3. dom88_rx7

    dom88_rx7 Well-Known Member

    Help again!!!
    it can be compiled but the java application(command prompt) stil got problem.

    p/s any1 have more examples of successful executed java code. any1 hv the ans for Java Programming From The Ground Up by McGraw-Hill International Edition's Ralph Bravaco and Shai Simonson

    here's another question

    Write a method that returns true if and only if an integer array contains duplicate items. Test this method in a program. Include a method that reads a list of numbers,terminated by -999,into an array.
    Write a method that returns the second largest value-stored in an array of type int. Write a main program that will read a list of numbers,detects whether there is any duplicate in the array and if there is none,find the second largest value stored in the array. You can make use of the functions written in (1)
    Method A-returns true if and only if an integer array contains duplicate items
    Method B-reads a list of numbers, terminated by-999, into an array
    Method C-returns the second largest value-stored in an array

    Write a method that accepts a string and displays another string composed of the characters of the first string but with all the lowercase letters capitalized. Any non-alphabetical characters such as punctuation,should be left unchanged. For example,the string
    “When Homer blew up the nuclear plant, he yelled !!###& DOH&&####!!!”
    Should become “WHEN HOMER BLEW UP THE NUCLEAR PLANT,HE YELLED !!###& DOH&&####!!!”

    Write another program that rotates a given string n characters to the right.
    For example,if the input of the program is “rotatemeplease”, the output should be “easerotatemepl”.
  4. spider-man

    spider-man Well-Known Member


    I don't really know how to use arrays.

    String word = input.nextLine();
    for(int i = 0; i < word.length; i++)
    word = word + word.charAt(i).touppercase();

    I think there you can use a method called char(int) . I think the int is represented by ASCII numbers. There is a range or number that is capital, and are symbols. You can use the number to restrict the .touppercase().
    Hope this helps.
  5. spider-man

    spider-man Well-Known Member


    I think I know how to do Q4.

    String word = input.nextLine();
    int toTheRight = input.nextInt();

    String wordy = word.subString(word.length() - toTheRight, word.length());
    String partWord = word.subString(0, toTheRight+1);

    String finally = wordy + partWord;

    You might have to +1 or -1 for some of the variables. I'm not sure :p
    #5 spider-man, Mar 1, 2010
    Last edited: Mar 1, 2010
  6. dom88_rx7

    dom88_rx7 Well-Known Member

    The attributes of a TV object are the channel, the volume and a flag(or switch) indicating whether the TV is on or off. The methods perform the following actions:
    a) Turn the TV on or off
    b) Set the channel to an integer from 0 to 99 inclusive
    c) Raise or lower the volume by one unit. The volume can range from 0 to 20
    d) View the value of the volume
    e) View the channel
    f) Determine whether the TV is on or off
    Write a TV class that implements all relevant functions. A newly created TV object is set to off with the channel set to 2 and the volume initially 10. Include a main(…) method that tests the methods of the TV class.
    #6 dom88_rx7, Mar 2, 2010
    Last edited: Mar 3, 2010
  7. spider-man

    spider-man Well-Known Member

    Could you post the solutions for all your questions, I want to look at them and learn. Thanks. :)
  8. spider-man

    spider-man Well-Known Member


    I don't know much. Trying to help. For the main mathod

    TV television = new TV();
    television. switch = false; // an attribute
    television. channel(33); // a method
    television. volume(11);
    television.getVolume(); //I think this is right
    television. getChannel(); //I think this is right
    television. isSwitch(); //I think this is right

    I don't know how to program the tv class. I feel confused. lol
    Can I have the solutions to this when you're done. Thanks:laugh2:
  9. All you need is a series of get/set methods.

    Using Jonathan Lam's method calls (with slight modifications):

    TV television = new TV(); // creates obj TV
    television. setSwitch = false;
    television. setChannel(33);
    television. setVolume(11);
    television. getChannel();
    television. isSwitch();

    /* **************************************************************** */
    /* * TV Class * */
    /* **************************************************************** */

    public class TV {

    protected boolean switch;
    protected int channel;
    protected int volume;

    public boolean isSwitch() {
    return switch;

    public int getChannel() {
    return channel;

    public int getVolume() {
    return volume;

    public void setSwitch(boolean switch) {
    this.switch = switch;

    public void setChannel(int channel) {
    if (channel >= 0 && channel <= 99) = channel;

    public void setVolume(int volume) {
    if (channel >= 0 && channel <= 20)
    this.volume = volume;

  10. spider-man

    spider-man Well-Known Member


    What does it return if I set the volume in the main class to be more than 20?
  11. that's up to you to decide.

    you have endless options. you can set an else, returning an error message of your choice, you can set it to 20 for any settings greater than 20.

    the action is your decision. it wasn't specified in the problem statement, so that's why i didn't include it.
  12. dom88_rx7

    dom88_rx7 Well-Known Member

    Oh Super Dan! Super Dan with Super Programming Skills. Thanx for ur help.
    But here comes trouble... Salute all of u here if can do tis

    (Designing with Classes and Objects)
    A computer game usually has many different objects that can be seen and manipulated. One typical object is a door. Whether a player runs through a castle, attacks the forces of an evil empire, or places furniture in a room, a door often comes into play.

    Implement a Door class as described below as well as a TestDoor class that instantiates three Door objects labelled “Enter”, “Exit” and “Treasure”. The “Enter” door should be left unlocked and opened. The “Exit” door should be left closed and locked. The “Treasure” door should be left open but locked.

    A Door class

    A Door object can
    • Display an inscription,
    • Be either open or closed, and
    • Be either locked or unlocked

    Here are some rules about how Door works.
    • Once the writing on the Door is set, it cannot be changed.
    • You may open the Door if and only if it is unlocked and closed.
    • You may close the Door if and only if it is open.
    • You may lock a Door if and only if it is unlocked, and unlock a Door if and only if it is locked. You should be able to check whether or not a Door is closed, check whether or not it is locked, and look at the writing on the Door if there is any.

    The instance variables of a Door class are:
    • String inscription
    • boolean locked, and
    • boolean closed.

    The methods (all public) should be:
    • Door(String c); //Constructor – initializes the Door with inscription c, closed and locked.
    • isClosed(); //Returns true if Door is closed.
    • isLocked(); //Returns true if Door is locked.
    • open() //Opens a Door if it is closed and unlocked.
    • lock() //Locks a Door if it is unlocked.
    • unlock(); //Unlocks a Door if it is locked.

    Appropriate error messages should be displayed, if any conditions of the methods are violated.
  13. dom88_rx7

    dom88_rx7 Well-Known Member

    and also tis

    Implement a class Employee such that a member Employee has name, an ID number, an age, a salary, a title, and a department name. An Employee can:
    a. Print a confidential employee record with all the above information.
    b. Change a salary (takes an int or a double argument). If the argument is int, then the salary is increased by that amount (a bonus addition, not a percent increase). If the parameter is double, then the salary is multiplied by the value of the argument and may increase or decrease depending on whether the double value is greater than or less than 1.0.
    c. getSalary().

    Implement a subclass Employee, called Manager. A manager is an employee who supervises other employees. A manager has a group of employees that he/she supervises. The confidential record of a manager includes all the information included in a regular employee’s confidential record plus a list of ID numbers of the employees that he/she supervises.

    Executive extends Manager. An executive is a manager who gets a bonus at the end of each year equal to a percentage of company profits. Implement Executive. You should redefine getSalary() to include the bonus. You should also add a method to change the percentage of the executive’s bonus.
  14. dom88_rx7

    dom88_rx7 Well-Known Member

    wat i need to do if i were to use object oriented programming (OOP) instead of conventional programming ?
  15. im pretty sure that all the languages you're learning in school are all OOP. i've always learned OOP so im not too sure what you mean by conventional programming.

    anyways, i don't want to write the program for you, because you wont learn anything from me doing the work for you. (trust me, i've nearly failed a midterm because i just copy pasted assignments from friends). try to actually do the assignment to the best of your abilities. (when i started working at IBM, if you asked me to program that using JAVA, i did not know a thing about Java.. but hands on work taught me). so you should attempt

    i'll just give you some guides and hints...

    the concept is pretty similar to the remote control example..

    1. you only need to make one Door class, since you can create 3 instances of the same class, for different purposes (enter, exit treasure)
    2. for each INSTANCE of the door class, you set the attributes so that it fits the requirements of that type of door. this is basically using the get/set methods like in the remote example.

    as for the inheritance problem, think of it this way.

    you have a person working at the store. they are an employee.

    a manager has the same information as an employee, (age, sex etc etc), but with additional info like a list of employees he/.she supervises.

    an exec is a manager, but with even MORE information, such as bonuses etc.

    the idea of inheritance states that to make a manager, you take the employee, and you extend it (ie. make him bigger ;D). to make an exec, you take a manager, and make (him even bigger ;D)

    so you will have a class called employee. this implements methods for the employee, ie, age, sex etc....

    for the manager, you EXTEND the employee, so you will have something like this:

    public class manager extends employee....

    in this manager class, you will have methods that are unique for a manager. this manager class will be able to use all methods within the employee class.

    the same with exec:

    public class executive extends manager

    this exec class will implement methods ONLY for the exec, however will be able to use any methods from the manager class, and subsequently, the employee class.

    now remember, for the actual methods for the classes, remember to use the concept of get/set methods, for each of the "attributes".... this will allow you to fetch the info required, and set them accordingly.

    good luck :)
  16. xtirpation

    xtirpation Active Member

    Shortest solutions I can think of (without the input-getting method)
    boolean duplicated (int [] array){
    	HashSet <Integer> checkSet = new HashSet<Integer>();
    	for (int i = 0; i < array.length; i++)
    	return (checkSet.size()==array.length);
    int secondLargest (int [] array){//Note: Assuming we can't change the original array.
    	int [] arrayCopy = Arrays.copyOf(array, array.length);
    	return arrayCopy[arrayCopy.length - 2];		
    String myUpperCase(String str){
    	return str.toUpperCase();
    String rotate (int n, String str){
    	return (str.substring(str.length()-n) + str.substring(0, str.length() - n));