The Help With Assignment Blog is intended to provide with tips and tricks to students so that they are able to do better at school and college. The Blog is associated with HelpWithAssignment.com (HwA), a leading provider of online tuitions in University subjects.

Showing posts with label java help with assignment. Show all posts
Showing posts with label java help with assignment. Show all posts

Friday, September 2, 2011

Java Subclassing and Inheritance With An Example

Java Subclassing and Inheritance

Classes in Java exist in hierarchy. A class in Java can be declared as a subclass of another class using the extends keyword. A subclass inherits variables and methods from its superclass and can use them as if they were declared within the subclass.

For example, if in a class,

class Animal {

float weight;

void eat() {

}

}

class Mammal extends Animal {

//inherits weight

int heartRate;

//inherits eat()

void breathe() {

}

}

In this example, an object of type Mammal has both the instance variable weight and the method eat(). They are inherited from Animal.

A class can extend only one other class. To use the proper terminology, Java allows single inheritance of class implementation. A subclass can be further subclassed. Normally, subclassing specializes or refines a class by adding variables and methods.

For example:

class Cat extends Mammal {

//inherits weight and inheritance

boolean longhair;

//inherits eat() and breathe()

void purr() {

}

}

The Cat class is a type of Mammal that is ultimately a type of Animal. Cat objects inherit all the characteristics of Mammal objects and in turn, Animal objects. Cat also provides additional behavior in the form of the purr() method and the long hair variable.

A subclass inherits all members of its superclass not designed as private. Other levels of visibility affect what inherited members of the class can be seen from outside of the class and its subclasses, but at a minimum, a subclass always has the same set of visible members as its parent. For this reason, a subclass always has the same set of visible members as its parent and instances of the subtype can be used anywhere instances of the supertype are allowed.

Another example,

Cat simon = new Cat ();

Animal creature = simon;

The Cat instance simon in the example can be assigned to the Animal type variable creature because Cat is a subtype of Animal. Similarly, any method accepting an Animal object would accept an instance of a cat or any Mammal type as well. This is an important aspect of polymorphism in an object-oriented language such as Java. This is used to refine a class’s behavior as well as add new capabilities to it.

For more details you can visit our websites at http://www.helpwithassignment.com/programing-assignment-help and http://www.helpwiththesis.com

Thursday, February 3, 2011

Java at HelpWithAssignment.com (Basic Date Formatting)

Basic Date Formatting

Let us see an example for Basic Date Formatting.

package com.ack.j2se.date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class BasicDateFormatting {
public static void main( String[] args ) throws Exception {
// get today's date
Date today = Calendar.getInstance().getTime();

// create a short version date formatter
DateFormat shortFormatter
= SimpleDateFormat.getDateInstance( SimpleDateFormat.SHORT );

// create a long version date formatter
DateFormat longFormatter
= SimpleDateFormat.getDateInstance( SimpleDateFormat.LONG );

// create date time formatter, medium for day, long for time
DateFormat mediumFormatter
= SimpleDateFormat.getDateTimeInstance( SimpleDateFormat.MEDIUM,
SimpleDateFormat.LONG );

// use the formatters to output the dates
System.out.println( shortFormatter.format( today ) );
System.out.println( longFormatter.format( today ) );
System.out.println( mediumFormatter.format( today ) );

// convert form date -> text, and text -> date
String dateAsText = shortFormatter.format( today );
Date textAsDate = shortFormatter.parse( dateAsText );
System.out.println( textAsDate );
}
}

Here in the above coding we can see that the program is used to fetch date. Date formats are of four types, Short, Medium, Long and Full. The above functions are used to call the date and date & time formats as well.
// get today's date
Date today = Calendar.getInstance().getTime(); initializes the date function. And the following line // create a short version date formatter
DateFormat shortFormatter
This and the following lines of code are written to include the different date and date time formats. The various date and date time formats are:
SHORT is completely numeric, such as 12.13.52 or 3:30pm
MEDIUM is longer, such as Jan 12, 1952
LONG is longer, such as January 12, 1952 or 3:30:32pm
FULL is pretty completely specified, such as Tuesday, April 12, 1952 AD or 3:30:42pm PST.

At HelpWithAssignment.com we provide the best Assignment help, Homework help and Online Tutoring in Java and other Programming languages. Our tutors are experts in Java and other Programming Languages like C, C++, C#, ASP.net, JSP.net, QBasic, Perl, Fortran, etc. The list is not exhaustive.

For more details you can visit our website at http://www.helpwithassignment.com and http://www.helpwithassignment.com/programing-assignment-help
This article is in contiuation with our previous articles on Java (Finding Factorial) and Java (Finding even and odd number)