Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


aidinkhalid
java oop
2 Answer(s)      3 years and 11 months ago
Posted in : Java Interview Questions


Design and implement a class called DayType that implements the day of the week in a program. The class DayType should store the day, such as Sun for Sunday, Mon for Monday and so on and so forth. The program should be able to perform the following operations on an object of type of DayType: Set the day Print the day Return the day Return the next day Return the previous day Calculate and return the day by adding and subtracting certain days to the current day (i.e if current day is Monday and we add 6 days, the day to be returned is Sunday. Similarly is we subtract 3 days, the day to be return is Friday)

instruction - Define DayType class appropriate methods and constructor - Your program should get the input/output via the main method and pass appropriate arguments through the class methods. - Your program should continuously running until it receives a special signal (character/number) in which the program will exit.


Advertisement
View Answers

February 4, 2011 at 2:37 PM


Hi Friend,

Try this:

public class DayType{
        final static int SUN = 0;
        final static int MON = 1;
        final static int TUE = 2;
        final static int WED = 3;
        final static int THU = 4;
        final static int FRI= 5;
        final static int SAT = 6;

        private int day;

        public DayType(int day) {
                this.day = day;
        }

        public void setDay(int day){
                this.day = day;
        }

        public int getDay() {
                return day;
        }

        public void print() {
                System.out.println(this.toString());
        }
        public int nextDay(){
                int next;
                next = day + 1;
                return next;
        }
        public int previousDay(){
                int prevDay;
                prevDay = day - 1;
                return prevDay;
        }
        public int addDays(int days) {
                return (day + days) % 7;
        }
                public String toString() {
                switch (this.day) {
                case SUN:
                        return "Sunday";
                case MON:
                        return "Monday";
                case TUE:
                        return "Tuesday";
                case WED:
                        return "Wednesday";
                case THU:
                        return "Thursday";
                case FRI:
                        return "Friday";
                case SAT:
                        return "Saturday";
                }
                return "";
        }       
        public static void main(String[] args) {
                System.out.println("******Test Day******");
                System.out.println();
                System.out.print("Set day: ");
                DayType d = new DayType(SUNDAY);
                d.print();
                System.out.print("Next day: ");
                d.setDay(d.nextDay());
                d.print();
                System.out.print("Previous day: ");
                d.setDay(d.previousDay());
                d.print();
                System.out.print("After 5 days: ");
                d.setDay(d.addDays(5));
                d.print();
        }       
}

Hope that it will be helpful for you.

Thanks



February 8, 2011 at 12:52 PM


hi member

ask something haves one error in public static void and daytypes d=newtypes(sunday) ; program said cannot have static declared you know what the solution



Related Tutorials/Questions & Answers:
java oop
java oop  Design and implement a class called DayType that implements the day of the week in a program. The class DayType should store the day, such as Sun for Sunday, Mon for Monday and so on and so forth. The program should
Java with OOP assignment (Eclipse)
Java with OOP assignment (Eclipse)  How do i control the length of characters in an input? E.g.: Enter your name > Hi * Name too short
Advertisements
OOP - Java Beginners
OOP Design Pattern  What is the oops design patterns
OOP JAVa - Java Interview Questions
OOP JAVa  Is java is 100% OOP.?Plz giv full explanation.  hi Sanjay,dis is Srinivas(MCA from Andhra University).....Java is not an 100 % OOPL,because of the availability of Primitive Data Types
oop answers - Java Beginners
oop answers  Consider the following declarations: { private int.... Write a Java statement that prints the values of the instance variables of x. i. Write a Java statement that creates the XClass object t and initializes
OOP questions - Java Beginners
OOP questions  Consider the following declarations: { private... of a and the instance variable w is initialized to the value of b. h. Write a Java statement that prints the values of the instance variables of x. i. Write a Java
OOP - Java Beginners
OOP  i have problem with OOP hope u'll help me write a program that allows the user to enter student' names followed by their test scores and outputs...://www.roseindia.net/java/master-java/java-object-oriented-language.shtml
OOP questions - Java Beginners
OOP questions  Consider the following declarations: { private int u; private double w; public XClass() { } public XClass(int a, double...; } For more information on Java visit to : http://www.roseindia.net/java
OOP with Java - Java Beginners
OOP with Java  I had a look at the solution for the answers, in the website already, but my lecturer wants us to do it in another method. Here goes...) { Question2 q = new Question2(); q.buildSeats(); q.printSeats
java"oop" - Java Beginners
Java OOPs Concept  What is OOPs programming and what it has to do with the Java?  Hi i hope you understand it.//To print the even numbers...){ System.out.println(e); } }}Java program to display all even numbers http
java oop - Java Beginners
java oop  Consider the following declarations: { private int u... variable w is initialized to the value of b. h. Write a Java statement that prints the values of the instance variables of x. i. Write a Java statement
JAVA OOP - Java Beginners
JAVA OOP  Employees in a company are divided into the classes Employee, HourlyPaid, Salescommissioned and Executive for the purpose of calculating their weekly wages or monthly salaries. The data to be maintained for each class
OOP - Java Beginners
OOP  Which one is better to learn object oriented progamming java or c++? thaks, Bet.  Hello, As of my concern both have different scope and use, you can not merge these. As i java person so i will show you java
OOP - Java Beginners
); } For more information on Java visit to : http://www.roseindia.net/java/ http://www.roseindia.net/java/beginners/Construct.shtml Thanks
OOP using Java - Java Beginners
OOP using Java  Can you write a Java statement that creates the object mysteryClock of the Clock type, and initialize the instance variables hr,min...); } } For more information on Java visit to : http://www.roseindia.net/java
OOP with Java 2 - Java Beginners
OOP with Java 2  Define a class called BogEntry that could be used to store an entry for a Web log. The class should have member variables to store the poster's username, text of entry, and the date of the entry using the Date
OOP Using JAVA - Java Beginners
OOP Using JAVA  OBJECT ORIENTED PROGRAMMING USING JAVA (hope guys u will help me please i need your help,thank you so much) Create a Java program...(); oops.OopType(); } } For more information on Java visit to : http
OOP with Java-Array - Java Beginners
OOP with Java-Array  Write a program to assign passengers seats in an airplane. Assume a small airplane with seat numberings as follows: 1 A B C D 2 A B C D 3 A B C D 4 A B C D 5 A B C D 6 A B C D 7
OOP with Java 3 - Java Beginners
OOP with Java 3  Write a Temperature class that has two instances variables: temperature value (a floating-point number) and a character for the scale, wither C for Celsius or F for Fahrenheit. the class should have four
oop
oop  can you tell me about why java is not 100% object oriented language
Java with OOP assignment (Eclipse) - Java Beginners
Java with OOP assignment (Eclipse)  "THREE Ts GAME" *Description* A "tic-tac-toe" game is a two player's board game where the game board..." games using Java. When your game application started, the players will be able
oop concept
oop concept  why Object class is super class for all class? 1.to use the Object class methods in subclass directly or 2.the Object class is usefull at the time of object create of subclass what
OOP
php oop functions
php oop functions  Just a quick question... php oop functions. can i call the main class function from an instantiated object of the child class
PHP OOP Concepts
PHP OOP Concepts Object Oriented Programming  is a paradigm which is nowadays the most popular way to develop any application and most of the modern day language is based on this paradigm. OOP or Object Oriented Programming PHP has
what is the difference between pop and oop
what is the difference between pop and oop  what is the difference between pop and oop
registration form using oop concept
registration form using oop concept  I would like to write a program student registration form at kindergartens and display the information of students that register to the kindergarten. How to make the form and display
OOP
OOP       OOPs stands... the object creation process, programmatically. Read more at : www.roseindia.net/java/beginners/oop-in-java.shtml
PHP OOP Namespace
Namespace in PHP: The term namespace is very much common in OOP based language, basically it is a collection of classes, objects and functions. Namespaces... should always declare on the top of the page. PHP OOP Namespace Example: <
Advertisements
 

 

 

DMCA.com