12 Replies - 1266 Views - Last Post: 08 February 2010 - 09:02 AM Rate Topic: -----

#1 Cooke3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 24-January 10

New to Java, don't understand everything

Posted 04 February 2010 - 09:56 PM

I've only ever coded in C++ and that was over a year ago for a class. Now my class is using Java and I'm having trouble connecting the different concepts and learning how the new ones work, it doesn't seem as intuitive as C++ and is confusing me a bit.

Anyway, here is code for a main program and an interface given by my teacher and we are supposed to fill in parts of it. It's supposed to find the day of the week of January 1st, 3000.

My questions:
1. Basically I would like to know what all this means, my teacher doesn't use comments to explain the code as much as my C++ teacher did so I find myself confused a lot.
2. Any hints on where to start?

import java.io.*;


/**
  Driver class for The Dating Game programming assignment.
  @author 
*/
public class Year3000
{
  public static void main(String [] args) throws IOException
  {
    DateInterface d = new MyDate();
    d.set(1,29,2006,0);  // sets the date to Sunday, January 29th, 2006
    while(d.getYear()<3000)
    {
      d.tomorrow();
    }
    // at this point, d represents January 1, 3000
    System.out.println(d);
  }
}




/**
Interface for Date objects to be used by the Year3000 driver program.
@author 
*/
public interface MyDate
{
  /** @return the day of the month (1-31) */
  public int getDay();
  /** @return the day of the week (0-6) */
  public int getDow();
  /** @return the month of the year (1-12) */
  public int getMonth();
  /** @return the year (four digits) */
  public int getYear();
  /** sets the date
      @param m the month of the year (1-12)
      @param d the day of the mongth (1-31)
      @param y the year (four digits)
      @param dow the day of the week (0-6) */
  public void set(int m, int d, int y, int dow);
  /** moves the date forward by exactly one day */
  public void tomorrow();
  /** @return the date as a String in the format "Monday March 18, 2002" */
  public String toString();

  /** sets the date to today;
      make this empty {} unless you do the extra credit. */
  public void today();
  /** Moves the date backword by exactly one day;
      make this empty {} unless you do the extra credit. */
  public void yesterday();
}





Thanks so much for any help!

Is This A Good Question/Topic? 0
  • +

Replies To: New to Java, don't understand everything

#2 Parad0x86  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 16
  • Joined: 02-January 10

Re: New to Java, don't understand everything

Posted 04 February 2010 - 10:24 PM

You'll find going from C++ to Java easy if you are interested in the subject since a lot of the syntax and concepts are essentially the same (and easier to understand i.e. no visible pointer arithmetic, this is done under the hood for you). Other than the hiding of system level concepts, the biggest difference between C++ and Java is that everything in Java must be written within a class. When I first went from C++ to Java I was super annoyed that I couldn't just write a general function without incorporating it with a class but as you progress you'll get the hang of it. If you just start thinking of everything as an object with states and behavior you will be off to a good start, and relax, the Java class structure is a lot cleaner than that of C++.

For your code, with an interface you are supposed to create a class and implement the interface and write your own version of the methods you want to use from the interface. It looks like your instructor screwed up and named the interface MyDate when he meant to name it DateInterface because MyDate being an interface makes "DateInterface d = new MyDate();" an illegal statement because you can't directly instantiate an interface; he should really double check his code before supplying it to new and unknowing students; if he did it on purpose hoping brand new students would find it then he is cruel and unusual.
http://java.sun.com/.../interface.html

This post has been edited by Parad0x86: 04 February 2010 - 10:57 PM

Was This Post Helpful? 1
  • +
  • -

#3 Cooke3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 24-January 10

Re: New to Java, don't understand everything

Posted 05 February 2010 - 04:54 AM

Oops. Actually that was my mistake. Another student told me that I needed to rename that to MyDate. So does that mean I need 3 different parts? The main program, DateInterface and MyDate. This all comes from not quite understanding what an interface is used for.
Was This Post Helpful? 0
  • +
  • -

#4 pdkharkar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Re: New to Java, don't understand everything

Posted 05 February 2010 - 10:02 AM

look
an interface is an abstract class
that is
it does not define any of the methods in the body
any class that implements the interface should define all the methods into it
Like parad0x86 has said, we cannot instantiate interface
that is in simple language
we cannot create the object of the interface
for more details about interfaces
check the same link as parad0x86 has provided
Was This Post Helpful? 0
  • +
  • -

#5 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: New to Java, don't understand everything

Posted 05 February 2010 - 10:07 AM

Here is a tutorial i made about Abstract classes, intefaces, and the difference between them
http://www.dreaminco...topic130490.htm
hope it would be useful :)
Was This Post Helpful? 0
  • +
  • -

#6 Cooke3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 24-January 10

Re: New to Java, don't understand everything

Posted 06 February 2010 - 12:07 PM

Ok, I've read the links provided and they were helpful in understanding it a little more. But as I'm a complete newbie it's hard to understand everything discussed without understanding the basics.

I edit my programs in Notepad++ and I've put the main and interface coding into one document then made another document called MyDate.java with public class MyDate, so this is where I need to write all the methods down?

Could someone please explain what the interface code means when it says:
/** sets the date
@param m the month of the year (1-12)
@param d the day of the mongth (1-31)
@param y the year (four digits)
@param dow the day of the week (0-6) */
public void set(int m, int d, int y, int dow);



and why there is a part for this:

/** moves the date forward by exactly one day */
public void tomorrow();

This post has been edited by Cooke3: 06 February 2010 - 12:07 PM

Was This Post Helpful? 0
  • +
  • -

#7 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: New to Java, don't understand everything

Posted 06 February 2010 - 12:22 PM

i see that the interface is called MyInterface, so it's not a good idea to call your class also MyDate.
also, the methods in an interface must be overriden inside the class that implement the interface.

public class MyDateClass implements MyDate {
//all methods in the MyDate interface should be here.




this method: public void set(int m, int d, int y, int dow);
will get a date while m is the month, d is the day y is the year and dow is the day of month.
so suppose you want to pass this date:
1/2/2010 friday, you will pass it:
MyDate dateObj = new MydateClass();
dateObj.set(1,2,2010,5);


same thing with the tommorow method.
Was This Post Helpful? 0
  • +
  • -

#8 oQMr FoxQo  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 123
  • Joined: 16-August 09

Re: New to Java, don't understand everything

Posted 06 February 2010 - 12:23 PM

View PostCooke3, on 06 February 2010 - 11:07 AM, said:

Could someone please explain what the interface code means when it says:
/** sets the date
@param m the month of the year (1-12)
@param d the day of the mongth (1-31)
@param y the year (four digits)
@param dow the day of the week (0-6) */
public void set(int m, int d, int y, int dow);


/** moves the date forward by exactly one day */
public void tomorrow();

mmmmm When your teacher is saying to you
@param m the month of the year (1-12)
means year can be:
1=january
2=february
etc...
@param dow the day of the week (0-6)
0=monday
1=tuesday
etc...
and so on and i beleive that the purpose of your poject is to output value of the next day (tomrrow()), next day and next day using the function d.tomorrow untill arriving to the year 3000


the function tomorrow is up to you to develop it in order to create this process
hope it is helpful...
oQMr FoXQo
Was This Post Helpful? 0
  • +
  • -

#9 Cooke3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 24-January 10

Re: New to Java, don't understand everything

Posted 06 February 2010 - 02:53 PM

Okay, could someone tell me if I am doing this part correctly? I have two Notepad++ documents now:


Is there anything I need to do in this part? Saved as Year3000.java.

/**
  Driver class for The Dating Game programming assignment.
  @author 
*/

import java.io.*;
import java.util.*;

public class Year3000
{
  public static void main(String [] args) throws IOException
  {
    DateInterface d = new MyDate();
    d.set(1,29,2006,0);  // sets the date to Sunday, January 29th, 2006
    while(d.getYear()<3000)
    {
      d.tomorrow();
    }
    // at this point, d represents January 1, 3000
    System.out.println(d);
  }
}


/**
Interface for Date objects to be used by the Year3000 driver program.
@author 
*/

public interface DateInterface
{
  /** @return the day of the month (1-31) */
  public int getDay();
  
  /** @return the day of the week (0-6) */
  public int getDow();
  
  /** @return the month of the year (1-12) */
  public int getMonth();
  
  /** @return the year (four digits) */
  public int getYear();
  
  /** sets the date
      @param m the month of the year (1-12)
      @param d the day of the mongth (1-31)
      @param y the year (four digits)
      @param dow the day of the week (0-6) */
  public void set(int m, int d, int y, int dow);
  
  /** moves the date forward by exactly one day */
  public void tomorrow();
  
  /** @return the date as a String in the format "Monday March 18, 2002" */
  public String toString();

  /** sets the date to today;
      make this empty {} unless you do the extra credit. */
  public void today();
  
  /** Moves the date backword by exactly one day;
      make this empty {} unless you do the extra credit. */
  public void yesterday();
}







This is the place where I'm going to write all the methods correct? Saved as MyDate.java

import java.io.*;
import java.util.*;

public class MyDate
{
	/** @return the day of the month (1-31) */
	public int getDay();
	
  
	/** @return the day of the week (0-6) */
	public int getDow();
  
	/** @return the month of the year (1-12) */
	public int getMonth();
  
	/** @return the year (four digits) */
	public int getYear();
  
	/** sets the date
	@param m the month of the year (1-12)
	@param d the day of the mongth (1-31)
	@param y the year (four digits)
	@param dow the day of the week (0-6) */
	public void set(int m, int d, int y, int dow);
 
	/** moves the date forward by exactly one day */
	public void tomorrow();
  
	/** @return the date as a String in the format "Monday March 18, 2002" */
	public String toString();

	/** sets the date to today;
	make this empty {} unless you do the extra credit. */
	public void today();
  
	/** Moves the date backword by exactly one day;
	make this empty {} unless you do the extra credit. */
	public void yesterday();
}



Was This Post Helpful? 0
  • +
  • -

#10 Cooke3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 24-January 10

Re: New to Java, don't understand everything

Posted 06 February 2010 - 03:22 PM

Oh and also for the tomorrow function will it be something like:

while (year<3000)
{day++}


Or something of that nature?
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,690
  • Joined: 27-December 08

Re: New to Java, don't understand everything

Posted 06 February 2010 - 03:50 PM

Your MyDate class should actually implement the DateInterface interface like so: public class MyDate implements DateInterface{. Remember how in C++ you have to prototype a method? In Java, an Interface is comparable to method prototyping. Then when you go to implement the interface in a class, you have to define the bodies of those methods. Also, you'll probably want to use some variables in your MyDate class, not just methods. In terms of your question about your tomorrow method, I would just increment the day variable by one (day++;), once you include a day variable in your MyDate class.
Was This Post Helpful? 0
  • +
  • -

#12 Guest_Sandeep*


Reputation:

Re: New to Java, don't understand everything

Posted 08 February 2010 - 08:03 AM

what are the conventional styles for writing class names,method names,constants and variables in JAVA?
can any one explain me....
coz i'm new to java...
Was This Post Helpful? 0

#13 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,690
  • Joined: 27-December 08

Re: New to Java, don't understand everything

Posted 08 February 2010 - 09:02 AM

Welcome to Dream.in.Code. For future reference, it is considered rude to hijack someone's thread for the purpose of asking your own question. It would be better (in the future) if you opened a new thread for your topic.

Here's your one freebie. :) Check out the link below for more information on your question.
http://java.sun.com/...tions.doc8.html
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1