1 Replies - 906 Views - Last Post: 06 November 2006 - 12:59 PM Rate Topic: -----

#1 AliBoss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-November 06

Used-Defined Classes question

Posted 06 November 2006 - 08:21 AM

Hello friends,

How are you?

I need some help with the output of my program. It is outputting null instead of the days required.

I have to make two classes, one the test class and the user defined class.

The requirements are:

Setting and Printing the day: which should be Sunday.

Return and print the next day.

Return and print the previous day.

Calculate and print the day after adding or substracting a number, for example today is Sunday, the program should prompt the user to enter a positive or negative number, if the number entered was 2, the new day has to be Tuesday.

Unfortunately, up to now, this is the output I get:

Today is: null
Tomorrow is: null
Yesterday was: null
Enter the days added with plus or substracted with minus
5
After 5 it will be null
Monday toString: first.TryDay@e83912
Day1 to Day2 equality false


Anyway, my user-defined class is:
package first;

public class TryDay 
{

	private String dayOfTheWeek;
	//private int anotherDay;
	//private int num;
	private String Monday, Tuesday, Wednsday, Thursday, Friday,
	Saturday, Sunday;
	private int DayValue;
	private int anotherDayValue;
	private int daysAdded;
	
	
	public TryDay()
	{
		setDay(1);
	}
	
	
	
	public TryDay(String str)
	{
		setDay(Sunday);
	}
	
	
	public TryDay(int num)
	{
		setDay(num);
	}
	
	public void setDay(String str)
	{
		int num = 0;
			
		if(num == 1)
		{
			dayOfTheWeek = Sunday;
		}
		
		else
			if(num == 2)
			{
				dayOfTheWeek = Monday;
			}
			else
				if(num == 3)
				{
					dayOfTheWeek = Tuesday;
				}
				else
					if(num == 4)
					{
						dayOfTheWeek = Wednsday;
					}
					else
						if(num == 5)
						{
							dayOfTheWeek = Thursday;
						}
						else
							if(num == 6)
							{
								dayOfTheWeek = Friday;
							}
							else
								if(num == 7)
								{
									dayOfTheWeek = Saturday;
								}
								
		
	}
	
	
	
	public void setDay(int num)
	{
			
		if(num == 1)
		{
			dayOfTheWeek = Sunday;
		}
		
		else
			if(num == 2)
			{
				dayOfTheWeek = Monday;
			}
			else
				if(num == 3)
				{
					dayOfTheWeek = Tuesday;
				}
				else
					if(num == 4)
					{
						dayOfTheWeek = Wednsday;
					}
					else
						if(num == 5)
						{
							dayOfTheWeek = Thursday;
						}
						else
							if(num == 6)
							{
								dayOfTheWeek = Friday;
							}
							else
								if(num == 7)
								{
									dayOfTheWeek = Saturday;
								}
								
		
	}
	
		
	public void setDayValue(String str)
	{
		setDay(str);
		if (dayOfTheWeek == Sunday)
		{
			DayValue = (1);
		}
		else
		if (dayOfTheWeek == Monday)
		{
			DayValue = (2);
		}
		else
			if(dayOfTheWeek == Tuesday)
			{
				DayValue = (3);
			}
			else
				if(dayOfTheWeek == Wednsday)
				{
					DayValue = (4);
				}
				else
					if(dayOfTheWeek == Thursday)
					{
						DayValue = (5);
					}
					else
						if(dayOfTheWeek == Friday)
						{
							DayValue = (6);
						}
						else 
							if(dayOfTheWeek == Saturday)
							{
								DayValue = (7);
							}
		
		
	}
	
	
	public void printDay()
	{
		System.out.println(dayOfTheWeek);
	}
			
	
	
		
	public String getDay()
	{
		return dayOfTheWeek;
	}
	
	public String getNextDay()
	{
		
		setDayValue(dayOfTheWeek);
		anotherDayValue = ++DayValue;
		setDay(anotherDayValue);
		return dayOfTheWeek;
	}
	
	public String getPreviousDay()
	{
		setDayValue(dayOfTheWeek);
		anotherDayValue = --DayValue;
		setDay(anotherDayValue);
		return dayOfTheWeek;
	}
	
	public String calculate(int addedDays) 
	{
		setDayValue(dayOfTheWeek);
		anotherDayValue = (DayValue + addedDays);
		setDay(anotherDayValue);
		return dayOfTheWeek;
	}

	
	//public boolean conmesBefore  
	//{
		
	//}
	
	
		
		
	}









And my test class is:

package first;

import java.util.*;

public class Assignment {
static Scanner console = new Scanner(System.in);
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int addedDays;
		String Monday, Tuesday, Wednsday, Thursday, Friday,
		Saturday, Sunday;

		
		
		TryDay Day1 = new TryDay("Sunday"); 
		
		TryDay Day2 = new TryDay("Monday");
		
		//boolean aboolean = Day1.comesBefore(Day2);
		
		//System.out.println("Testing whether " + Day1 + " comes before " + Day2 + ": " + aboolean);
		
		
		
		
		System.out.print("Today is: " + Day1.getDay());
		//Day1.printDay();
		System.out.println();
		
		
		System.out.print("Tomorrow is: " + Day1.getNextDay());
		//Day2.printDay();
		System.out.println();
		
		System.out.println("Yesterday was: " + Day1.getPreviousDay());
		
		System.out.println("Enter the days added with plus or substracted with minus ");
		
		addedDays = console.nextInt();
		
		
		
		
		System.out.println("After " + addedDays + " it will be " + Day1.calculate(addedDays));
			   
		System.out.println("Monday toString: " + Day2.toString());
		
		//System.out.println("4 days ago it was be " + Day2.calculate(-4));
		
		System.out.println("Day1 to Day2 equality " + Day1.equals(Day2));

		
		
		
		
		
		
		
		
		
		
	}
	
	
	
}




The statements that I wrote "//" before them, means that I don't know how to make the code to make them work, so please if anyone can help me with the output and with the ones that I still don't know hw to write the write code for them, I would be appreciate it.


By the way, there was a similar question posted here long time ago, and I used some of the codes in that program, it helped me a bit.

edit: added [code] tags ~ jayman9

Is This A Good Question/Topic? 0
  • +

Replies To: Used-Defined Classes question

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Used-Defined Classes question

Posted 06 November 2006 - 12:59 PM

It appears as if you are declaring strings to represent the days, but you never assign any string values to those strings objects. For example, when to call the TryDay constructor with a value of "Sunday", in the constructor you are assigning the value of Sunday - the object, not the provided string. you are providing String str to the constructor - that is the value you should be assigning.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1