Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

 nested array in java, assign a single array into another array

views
     
TSmirror1106
post Aug 28 2008, 12:45 PM, updated 18y ago

New Member
*
Newbie
2 posts

Joined: Aug 2008
Hi everyone....
i m new to java....
i have a project of creating a simple application calendar with the features of add /delete appointment and so on....

below r my coding

import java .util.*;
import java .util.Scanner;
import java.lang.Object;
import java.lang.*;


//_______________________________________________________________________________________________________________________________________________________
//public
class Calendar {
private Month[] months = new Month[12];
public static int year;
static final String J="Jan",F="Feb",M="Mar",A="Apr",m="May",j="Jun",U="Jul",a="Aug",S="Sep",O="Oct",N="Nov",D="Dec";
static Scanner sc=new Scanner(System.in);

public Calendar()
{
year=0;
}
public Calendar(int yr)
{
year=yr;
}

public int getYear()
{
return year;
}
public void createYearView(int yr){

System.out.println("\t <><><><><><><><><><><><><>");
System.out.println("\t Calendar " + yr );
System.out.println ("\t <><><><><><><><><><><>\n");
System.out.print ("\t 1. "+J);
System.out.print ("\t 2. "+F);
System.out.print ("\t 3. "+M);
System.out.println ("\t 4. "+A);
System.out.print ("\t 5. "+m);
System.out.print ("\t 6. "+j);
System.out.print ("\t 7. "+U);
System.out.println ("\t 8. "+a);
System.out.print ("\t 9. "+S);
System.out.print ("\t10. "+O);
System.out.print ("\t11. "+N);
System.out.println ("\t12. "+D);


}

}

//_________________________________________________________________________________________________________________________________________________________
//public
class Month {

private Day[] days;
static final String J="Jan",F="Feb",M="Mar",A="Apr",m="May",j="Jun",U="Jul",a="Aug",S="Sep",O="Oct",N="Nov",D="Dec";
//public boolean addAppointment(int day, int hr, int min, String description) {
// return days[day].addAppointment(hr, min, description);
// }


public int setLength(){
return days.length;
}

public void createMonthView(int mth,int yr,int length) {
System.out.println ("\t <><><><><><><><><><><>");
switch (mth)
{ case 1:System.out.println ("\t\t"+ J +"\t "+yr); break;
case 2:System.out.println ("\t\t"+ F +"\t "+yr); break;
case 3:System.out.println ("\t\t"+ M +"\t "+yr); break;
case 4:System.out.println ("\t\t"+ A +"\t "+yr); break;
case 5:System.out.println ("\t\t"+ m +"\t "+yr); break;
case 6:System.out.println ("\t\t"+ j +"\t "+yr); break;
case 7:System.out.println ("\t\t"+ U +"\t "+yr); break;
case 8:System.out.println ("\t\t"+ a +"\t "+yr); break;
case 9:System.out.println ("\t\t"+ S+"\t "+yr); break;
case 10:System.out.println ("\t\t"+ O +"\t "+yr); break;
case 11:System.out.println ("\t\t"+ N +"\t "+yr); break;
case 12:System.out.println ("\t\t"+ D +"\t "+yr); break;
}
System.out.println ("\t <><><><><><><><><><><>\n");

for(int i=1;i<=7;i++)
System.out.print("\t "+i+"\t" );
System.out.println();

for(int i=8;i<=14;i++)
System.out.print("\t "+i+"\t");
System.out.println();

for(int i=15;i<=21;i++)
System.out.print("\t "+i+"\t");
System.out.println();

for(int i=22;i<=length;i++)
System.out.print("\t "+i+"\t");
System.out.println();
}
}




//_________________________________________________________________________________________________________________________________
//public
class Day {

private static int MAXAPPTS = 10;
private Appointment[] appts=new Appointment[MAXAPPTS];
public int numAppts=0;


public Day()
{
for(int i=0;i<MAXAPPTS;i++)
appts[i]=new Appointment();

}

public void isFull(){
if(numAppts>=MAXAPPTS)
System.out.println("You are not allowed to add appoinment in a day which the number of appointment is more than 10");

}

public void addAppointment(int hr, int min, String description,int num) {
isFull();
appts[num].setAppointment(hr, min, description);


}

public void printAppointment(Appointment ... appts)
{

for(int i=0;i<appts.length;i++)
{
if(appts[i].getHour()<10)
System.out.print("\t 0");
System.out.print(appts[i].getHour() + ":");

if(appts[i].getMinute()<10)
System.out.print("0");
System.out.print(+appts[i].getMinute()+"\t");
System.out.println(appts[i].toString());


}
}


}
//_____________________________________________________________________________________________

class Appointment{
private int hour;
private int minute;
private String descriptioN;

public Appointment()
{
setAppointment(0,0,null);
}

public Appointment(int hr, int min, String description)
{
setAppointment(hr,min,description);
}


public void setAppointment( int hr, int min, String description){
if(0<=hour && hour <24)
hour =hr;
else
hr=0;
if (0<=minute &&minute <60)
min=minute;
else
min=0;

descriptioN=description;

}

public int getHour(){
return hour;
}

public int getMinute(){
return minute;
}

public String toString(){
return descriptioN;
}



public void printAppointment()
{
if(hour<10)
System.out.print("0");
System.out.print(hour + ":");

if(minute<10)
System.out.print("0");
System.out.print(minute+"\t");
System.out.println(descriptioN);

}
}



//_______________________________________________________________________________________________________________________________________
public class Application{

private static Scanner sc=new Scanner(System.in);
static final String J="Jan",F="Feb",M="Mar",A="Apr",m="May",j="Jun",U="Jul",a="Aug",S="Sep",O="Oct",N="Nov",D="Dec";


public void createYearView(int yr){
System.out.println ("\t <><><><><><><><><><><>");
System.out.println ("\t Calendar "+yr);
System.out.println ("\t <><><><><><><><><><><>\n");
System.out.print ("\t 1. "+J);
System.out.print ("\t 2. "+F);
System.out.print ("\t 3. "+M);
System.out.println ("\t 4. "+A);
System.out.print ("\t 5. "+m);
System.out.print ("\t 6. "+j);
System.out.print ("\t 7. "+U);
System.out.println ("\t 8. "+a);
System.out.print ("\t 9. "+S);
System.out.print ("\t10. "+O);
System.out.print ("\t11. "+N);
System.out.println ("\t12. "+D);
}

public static void mainMenu()
{
System.out.println ("\t <><><><><><><><><><><>");
System.out.println ("\t WELCOME TO MyCal ");
System.out.println ("\t <><><><><><><><><><><>\n\n");

System.out.println ("\t What do you want to do? ");
System.out.println ("\t [1].Create a new calendar? ");
System.out.println ("\t [2].Open a calendar ");
System.out.println ("\t [0].Exit MyCal ");
System.out.print ("\t Selection: ");
}

public static void exitMycal()
{
System.exit(0);
}

public static void whatDoYouWantToDo()
{
System.out.println("\t [1-12] Choose a month . ");
System.out.println("\t [13] Add an appointment ");
System.out.println("\t [0] Exit Mycal ");
System.out.print("\t Selection:");
}

public static void whatDoYouWantToDo_1()
{
System.out.println("\t [1-12] Choose a day ");
System.out.println("\t [0] Exit Mycal ");
System.out.print("\t Selection:");
}

public static void whatDoYouWantToDo_2()
{
System.out.println("\t [1] Make a new appointment ");
System.out.println("\t [2] Delete an appointment ");
System.out.println("\t [0] Exit Mycal ");
System.out.print("\t Selection:");
}

public static void whatDoYouWantToDo_3()
{
System.out.println("\t [1] Back to main menu ");
System.out.println("\t [0] Exit Mycal ");
System.out.print("\t Selection:");
}

public static void whatDoYouWantToDo_4()
{
System.out.println("\t [1-12] Choose a day ");
System.out.println("\t [0] Exit Mycal ");
System.out.print("\t Selection:");
}

public static int getTotalDay(int mth,int yr)
{
switch(mth)
{
case 0:case 2:case 4:case 6:case 7:case 9:case 11: return 31;

case 3: case 5: case 8: case 10:
return 30;

case 1:
if(yr%4 !=0)
return 28;
else if (yr %400==0)
return 29;
else if (yr%100==0)
return 28;
else return 29;

default:
System.out.println("Error,Invalid month index= " +mth);
return -1;
}
}
//////////////////////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
public static void main (String[] args)
{
int month,year=2008;
int[][] time=new int [12][];

time[0]=new int [getTotalDay(0,year)];
time[1]=new int [getTotalDay(1,year)];
time[2]=new int [getTotalDay(2,year)];
time[3]=new int [getTotalDay(3,year)];
time[4]=new int [getTotalDay(4,year)];
time[5]=new int [getTotalDay(5,year)];
time[6]=new int [getTotalDay(6,year)];
time[7]=new int [getTotalDay(7,year)];
time[8]=new int [getTotalDay(8,year)];
time[9]=new int [getTotalDay(9,year)];
time[10]=new int [getTotalDay(10,year)];
time[11]=new int [getTotalDay(11,year)];

for(int i=0;i<12;i++){
for (int j=0;j<time.length;j++){
time[i][j]=0;
}
}

}
}


i use ragged array ,but i faced problem like each day can only save an appointment .
my question is how do i assignment a single array of day into array of month and how do i assign a single array of appointment into array of day....
anyone konw how to solve tis problem?


















dopodplaya
post Aug 28 2008, 12:55 PM

Look at all my stars!!
*******
Senior Member
2,280 posts

Joined: Jun 2006
Post here - http://forum.lowyat.net/Codemasters
this is clearly a different section.
chris_c28
post Aug 28 2008, 10:06 PM

Enthusiast
*****
Senior Member
882 posts

Joined: Jan 2005


Not sure if I understand your question correctly, but I'll try.
Can't you just iterate through your months and assign your day array into each? When you do a simple assignment, you're only passing the reference, so it's a cheap operation.

Also, use ArrayList if possible. Dynamic resizing and generics support.

This post has been edited by chris_c28: Aug 28 2008, 10:07 PM

 

Change to:
| Lo-Fi Version
0.0177sec    0.42    5 queries    GZIP Disabled
Time is now: 22nd December 2025 - 03:03 AM