Friday, October 28, 2011

Java Classes


Classes
No_Constructors

1.  Write the definition of a class Simple.  The class has no constructors, methods or instance variables.

public class Simple{}


2.  Write the definitions of two classes Day and Night.  Both classes have no constructors, methods or instance variables.

Note:  For this exercise, please do not use declare your classes using the public visibility modifier.

class Day{}
class Night{}


Instance_Variables

1.  Write the definition of a class Clock.  The class has no constructors and one instance variable of type int called hours.

public class Clock {
private int hours;
}


2.  Write the definition of a class Clock.  The class has no constructors and two instance variables.  One is of type int called hours and the other is of type boolean called isTicking.

public class Clock {
private int hours;
private boolean isTicking;
}


3.  Write the definition of a class Clock.  The class has no constructors and three instance variables.  One is of type int called hours, another is of type boolean called isTicking, and the last one is of type Integer called diff.

public class Clock {
private int hours;
private boolean isTicking;
private Integer diff;
}

4.  Write the definition of a class Clock.  The class has no constructors and three instance variables.  One is of type int called hours, initialized to 12 , another is of type boolean called isTicking, and the last one is of type Integer called diff.

class Clock{private int hours=12;private boolean isTicking;private Integer diff;}


5.  Write the definition of a class Clock.  The class has no constructors and three instance variables.  One is of type int called hours, initialized to 12, another is of type boolean called isTicking, initialized to true, and the last one is of type Integer called diff, initialized to 5.

class Clock {

private int hours = 12 ;
private boolean isTicking = true ;
private Integer diff = 5 ;
}


Constructors

1.  You are given a class named Clock that has one int instance variable called hours.  Write a constructor with no parameters for the class Clock.  The constructor should set hours to 12.

public Clock(){
hours = 12 ;
}


2.  You are given a class named Clock that has one  int instance variable called  hours.  Write a constructor for the class Clock that takes one parameter, an int , and gives its value to hours.

public Clock(int hours){
this.hours = hours ;
}


3.  You are given a class named Clock that has three instance variables:  One of type int called hours, another of type boolean called isTicking, and the last one of type Integer called diff.  Write a constructor for the class Clock that takes three parameters -- an int , a boolean, and another int.  The constructor should set the instance variables to the values provided.

public Clock (int hours, boolean isTicking , int diff)
{
this.hours = hours ;
this.isTicking = isTicking ;
this.diff = new Integer (diff) ;
}


4.  A class named Clock has two instance variables:  hours (type  int ) and isTicking (type boolean).  Write a constructor that takes a reference to an existing Clock object as a parameter and copies that object's instance variables to the object being created.

Your task:  Write a Clock constructor that makes this possible.  Just write this constructor -- don't write the whole class or any code outside of the class!
Example:
For example, if cl is a reference to a Clock object, whose hours and isTicking values are 10 and false respectively, the expression new Clock(cl) results in a new Clock object, whose instance variables' values are also 10 and false.

public Clock (Clock existingClock)
{
hours = existingClock.hours;
isTicking = existingClock.isTicking;
}


5.  Write the definition of a class Clock.  The class has three instance variables:  One of type int called hours, another of type boolean called isTicking, and the last one of type Integer called diff.  You should also write a constructor that takes three parameters -- an int, a boolean, and another int.  The constructor should set the instance variables to the values provided.

public class Clock { private int hours; private boolean isTicking; private Integer diff; public Clock(int hours, boolean isTicking, int diff) { this.hours=hours; this.isTicking=isTicking; this.diff = (int) diff; } }


Static_Members

1.  Write the definition of a class Telephone.  The class has no constructors, one instance variable of type String called number, and one static variable of type int called quantity.

public class Telephone{
private String number ;
private static int quantity ;
}


2.  Write the definition of a class Telephone.  The class has no constructors, one instance variable of type String called number, and two static variables.  One is of type int called quantity, initialized to 250; the other is of type double called total, initialized to 15658.92.

public class Telephone{
private String number;
private static int quantity = 250;
private static double total = 15658.92;
}


3.  Write the definition of a class Telephone.  The class has no constructors and one static method printNumber.  The method accepts a String argument and prints it on the screen.  The method returns nothing.

public class Telephone
{
public static void printNumber(String number)
{
System.out.println(number) ;
}
}


4.  Write the definition of a class Telephone.  The class has no constructors and one static method getFullNumber.  The method accepts a String argument and returns it, adding 718- to the beginning of the argument.

public class Telephone
{
public static String getFullNumber(String s)
{
return ("718-" + s) ;
}
}


5.  Write the definition of a class Telephone.  The class has no constructors, one instance variable of type String called number, and two static variables.  One is of type int called quantity; the other is of type double called total.  Besides that, the class has one static method makeFullNumber.  The method accepts two arguments, a String containing a telephone number and an int containing an area code.  The method concatenates the two arguments in the following manner:  First comes the area code, then a dash, then the telephone number.  The method returns the resultant string.

public class Telephone
{
private String number;
private static int quantity;
private static double total;
public static String makeFullNumber(String telephone, int area)
{
return (area + "-" + telephone);
}
}


ClassDefinitions

Book

1.  Write a class named Book containing:
Two instance variables named title and author of type String.
A constructor that accepts two String parameters.  The value of the first is used to initialize the value of  title and the value of the second is used to initialize  author.
A method named toString that accepts no parameters.  toString returns a String consisting of the value of title, followed by a newline character, followed by the value of  author.

public class Book
{
private String title;
private String author;
public Book (String title, String author)
{
this.title = title;
this.author = author;
}
public String toString()
{
return title+ "\n" + author;
}
}


2.  Write a class named Book containing:
Three instance variables named title, author, and tableOfContents of type String.  The value of tableOfContents should be initialized to the empty string.
An instance variable named nextPage of type int, initialized to 1.
A constructor that accepts two String parameters.  The value of the first is used to initialize the value of title and the value of the second is used to initialize author.
A method named addChapter that accepts two parameters.  The first, of type String, is the title of the chapter; the second, is an integer containing the number of pages in the chapter.  addChapter appends (that is concatenates) a newline followed by the chapter title followed by the string "..." followed by the value of the nextPage instance variable to the tableOfContents.  The method also increases the value of nextPage by the number of pages in the chapter.
A method named getPages that accepts no parameters.  getPages returns the number of pages in the book.
A method named getTableOfContents that accepts no parameters.  getTableOfContents returns the values of the tableOfContents instance variable.
A method named toString that accepts no parameters. toString returns a String consisting of the value of title, followed by a newline character, followed by the value of author.

public class Book{
private String title;
private String author;
private String tableOfContents = "";
private int nextPage = 1;
public Book (String a , String b){
this.title = a;
this.author = b;
}
public void addChapter (String chapterTitle, int number){
tableOfContents += "\n" + chapterTitle + "..." + nextPage;
nextPage += number;
}
public int getPages(){
return nextPage-1;
}
public String getTableOfContents(){
return tableOfContents;
}
public String toString(){
return title + "\n" + author;
}
}


ParkingMeter

1.  Write a class named  ParkingMeter containing:
An instance variable named timeLeft of type int, initialized to 0.
A method named  add that accepts an integer parameter. If the value of the parameter is equal to 25, the value of  timeLeft is increased by 30; otherwise no increase is performed.  add returns a boolean value:  true if  timeLeft was increased,  false otherwise.
A method named tick that accepts no parameters and returns no value.  tick decreases the value of timeLeft by 1, but only if the value of  timeLeft is greater than 0.
A method named isExpired that accepts no parameters.  isExpired returns a boolean value: true if the value of timeLeft is equal to 0; false otherwise.

public class ParkingMeter{
private int timeLeft = 0;
public boolean add (int x){
if (x == 25)
{
timeLeft += 30;
return true;
}
else
{
return false;
}
}
public void tick(){
if (timeLeft > 0)
{
timeLeft--;
}
}
public boolean isExpired(){
if (timeLeft == 0)
{
return true;
}
else
{
return false;
}
}
}


2.  Write a class named  ParkingMeter containing:
Two instance variables named timeLeft and maxTime of type int.  The value of  timeLeft should be initialized to 0.
A constructor accepting a single integer parameter whose value is used to initialize the maxTime instance variable.
A method named add that accepts an integer parameter.  If the value of the parameter is equal to 25, the value of timeLeft is increased by 30; otherwise no increase is performed.  Furthermore, the increase occurs only if the value of timeLeft will not exceed the value of  maxTime.  add returns a boolean value:  true if timeLeft was increase, false otherwise.
A method named tick that accepts no parameters and returns no value.  tick decreases the value of  timeLeft by 1, but only if the value of  timeLeft is greater than 0.
A method named isExpired that accepts no parameters.  isExpired returns a boolean value: true if the value of  timeLeft is equal to 0; false otherwise.

public class ParkingMeter{
private int timeLeft = 0 ;
private int maxTime;
public ParkingMeter(int x) {
this.maxTime=x;
}
public boolean add (int y){
if ((y == 25) && (timeLeft < maxTime))
{
timeLeft += 30;
return true;
}
else
{
return false;
}
}
public void tick() {
if (timeLeft > 0)
{
--timeLeft;
}
}
public boolean isExpired(){
if(timeLeft==0)
{
return true;
}
else
{
return false;
}
}
}








No comments:

Post a Comment