Java Array of Objects How to Access and Print - java

I am entering data into two classes (Date and Task) that are array of objects (I believe that is what they are officially called) and part of a Day class. I can add data to the Task class using the setter and I can add data to the Date class using the setter. The question I have is how do I access that data entered into the Date and Task classes through the Day class that contains those objects. I am using the Tester class to enter the data into these classes. I can access and print the data for each date/task(s) combo right after I add the data to the classes by using the "System.out.println(day1);". However, I can only print the last data I entered into the classes. When I try to access the objects' data through the Day class, it is not showing anything.
I would expect my results to be the following:
2022/2/23
Description: Write code Hours: 2.5
Description: Research Hours: 1.5
For a total of 4.0 hours.
2022/2/24
Description: Read code Hours: 5.0
For a total of 5.0 hours.
But instead I just get the following error when I try to compile it using JGrasp:
Tester.java:28: error: cannot find symbol
for (i=0; i < day1.length; i++) {
^
symbol: variable length
location: variable day1 of type Day
Tester.java:29: error: incompatible types: String cannot be converted to Task[]
array = day1.getDate();
^
2 errors
How do I print out/access the objects in the Day class? I'm confused about how to do this in this context. When I use "System.out.println(day1);" I was assuming it would print out both of the sets of data I entered into the classes, but only the most recent one printed. I can print off the first data set entered by putting "System.out.println(day1);" after the first data set, but that is the only way it will print. It almost seems like I am writing over the top of the first set of data I add with the second set of data. Add to that I'm pretty unsure about how objects embedded within objects work. Thanks for your help! Here is the code:
public class Day implements Comparable {
public static final int MAX=50;
private Task[] tasks;
private int numTasks;
Date myDate;
Day myDay;
public Day(int year, int month, int day) {
myDate = new Date(year, month, day);
tasks = new Task[MAX];
numTasks = 0;
}
public void addTask(Task newTask) {
tasks[numTasks] = newTask;
numTasks++;
}
public int getNumTasks() {
return numTasks;
}
public String getDate() {
return myDate.toString();
}
public double getTotalHours() {
int i;
double sum;
sum = 0;
for (i=0; i < numTasks; i++)
sum += tasks[i].getHours();
return sum;
}
public String toString() {
String result;
int i;
result = myDate.toString()+"\n";
for (i=0; i < numTasks; i++)
result += tasks[i].toString()+"\n";
result += "For a total of "+getTotalHours()+" hours.";
return result;
}
//other getters and setters in this class too
}
public class Date implements Comparable {
private int year, month, day;
public Date (int inYear, int inMonth, int inDay) {
year = inYear;
month = inMonth;
day = inDay;
if (day > getNumDaysMonth(month, year) ||
month < 1 || month > 12) {
year = -1;
month = -1;
day = -1;
}
//other getters and setters below this
}
}
public class Task implements Comparable{
// field variables
private String description;
private double hours;
// constructors -> initialize all field variables - specialized method
public Task(String desc, double hrs) {
description = desc;
hours = hrs;
}
//other getters and setters below this
}
import java.util.Arrays;
public class Tester {
public static void main(String[] args) {
Day day1;
Task task1;
Task array[];
Day array2[];
double totalHours;
int n, i;
day1 = new Day(2022, 2, 23);
task1 = new Task("Write code", 2.5);
day1.addTask(task1);
task1 = new Task("Research", 1.5);
day1.addTask(task1);
n = day1.getNumTasks();
System.out.println("Number of tasks = "+n);
day1 = new Day(2022, 2, 24);
task1 = new Task("Read code", 5.0);
day1.addTask(task1);
System.out.println(day1);
System.out.println();
for (i=0; i < day1.length; i++) {
array = day1.getDate();
System.out.println(array);
}
totalHours = day1.getTotalHours();
System.out.println("Total hours = "+totalHours);
}
}

You're assigning "day1.getDate()", which is a String, to "array", which is a task array. These are not compatible. All you need to do is System.out.println(day1.getDate()); That would only print the same date day1.length times though. I suppose you want something from the day1 tasks. You probably want to implement a Task Day.getTask(int index) method to access them. It would be bad practice to return the whole task array, since the caller could then modify it. If you did want to implement a Task[] Day.getTasks() method, it should return a copy of the task array.

Related

How can a User create dates in my Calender (java)?

I m currently trying to code a Calender with java.
I created 3 classes:
1. Date( includes year, month....)
2. Event(includes people, place, the class Date ... + an option to create dates )
3. Mainclass My mainclass that contains the menu.
My problem is that I don't know how the user is able to create his own date, because I have to create the object Termin myself... So, can somebody help me fix this? Thx in advance!
public class Event {
private String mDescription, mPlace, mNames;
private Date mStart, mEnd;
Termin(String description, String place, String names, Date start, Date end) {
mBetreff = description;
mOrt = place;
mNamen = names;
mBeginn = start;
mEnde = end;
}
public void create() {
Scanner read = new Scanner(System.in);
System.out.println("Enter 1. description 2. place 3. names 4. start 5. end ein");
mDescription = read.nextLine();
mPlace = read.nextLine();
mNames = read.nextLine();
}
public String toString() {
return "Description : " + mDescription + "\nPlace: " + mPlace + "\nNames: " + mNames + "\nIts starts at " + mStart
+ " and ends at " + mEnd;
}
}
public class Date {
private int year, day, month, hours, minutes;
Datum(int year, int month, int day, int hours, int minutes) {
this.day= day;
this.year= year;
this.month= month;
this.hours= hours;
this.minutes= minutes;
}
public String toString() {
return "\n" + day + "." + month + "." + year + " um " + hours+ ":" + minutes;
}
public void enterDate() {
}
}
EDIT:
I asked this question 2 years ago, back when I just started coding and had no idea of oop and encapsulation ...
To answer my own question, for every newbie that also tries to create a terminal calender:
Date needs the following methos:
public setDate() {
this.year = read.nextLine();
...
}
for every member.
Event takes the resulting object Date, either in the constructor or in a setter like method.
Creating an instance-method to create an appointment is kind of... strange since one needs to create an appointment (called Termin in your case) to create an appointment. One possibility would be the builder pattern. By having a public static inner builder class, you can set the constructor(s) private and enforce the use of that builder:
public class Main {
private int value;
private Main(int value) {
this.value = value;
}
public int getValue() {
return (this.value);
}
public static class MainBuilder {
boolean valueWasSet;
int value;
public MainBuilder() {
this.valueWasSet = false;
this.value = -1;
}
public void setValue(int value) {
this.value = value;
this.valueWasSet = true;
}
public Main build() {
if (!this.valueWasSet) {
throw new IllegalStateException("value must be set before a Main can be build.");
}
return (new Main(this.value));
}
}
}
(this is a simplified sketch to show the core mechanism on how to assert that certain values are set before constructing a Main through MainBuilder.
The process of constructing a Main would be:
MainBuilder builder = new MainBuilder();
builder.setValue(100);
// all following Main's will have a value of 100
Main mainOne = builder.build();
Main mainTwo = builder.build();
builder.setValue(200);
// all following Main's will have a value of 200
Main mainThree = builder.build();
Main mainFour = builder.build();

Several errors keep occurring in eclipse

I'm trying to write a program that opens a txt file and display information from that txt file. Java is my first language, and I'm taking java as a second language class since there's no beginning java class in my school. I'm struggling with this code for about an week. Any little help would be helpful. Appreciate for your help.
It keeps saying :
Exception in thread "main"java.lang.ArrayIndexOutOfBoundsException:6
at store.Franchise.<init>(Franchise.java:10)
at store.FileIO.readData(FileIO.java:10)
at store.Driver.main(Driver.java:9)
Here is what I've got:
Sample txt file:
Day1 Day2 Day3 Day4 Day5
2541.56 2258.96 2214 2256 2154
2041.56 1758.96 1714 1756 1654
3041.56 2758.96 2714 2756 2654
3563.54 3280.94 3235.98 3277.98 3175.98
2547.21 2264.61 2219.65 2261.65 2159.65
4040.55 3757.95 3712.99 3754.99 3652.99
Store.java:
package store;
import java.io.IOException;
public class Store {
private float salesByWeek[][];
public Store() {
salesByWeek = new float[5][7];
// assign the array value at index 5, t to salesByWeek
}
public void setSaleForWeekDayIntersection(int week, int day, float sale) {
salesByWeek[week][day] = sale;
// store the sale value to SalesByWeek array at the index pointed to by week, day
// for exaample, it can be week 2 and day 3 (Wednesday)
}
float[] getSalesForEntireWeek(int week) {
// this will find the total sales for the whole week - all 5 days or 7 days including week ends Saturday and Sunday
float[] sales = new float[7];
// declare an array of type float and of size 7 - name the array as sales
for (int d = 0; d < 7; d++)
{
sales[d] = salesByWeek[week][d];
// the index d runs from 0 to 7
}
return sales;
}
float getSaleForWeekDayIntersection(int week, int day) {
return salesByWeek[week][day];
// the return value is the arraycontent pointed to by index week and day
}
float getTotalSalesForWeek(int week) {
float total = 0;
for (int d = 0; d < 7; d++)
{
total += salesByWeek[week][d];
// increment total by adding the array content salesByWeek at index week, d ( if d is the day)
}
return total;
// send the value of total back to the caller function
}
float getAverageSalesForWeek(int week) {
return getTotalSalesForWeek(week) / 7;
// divide the total sales for the whole week by 7 so that we get the average sales and return it
}
float getTotalSalesForAllWeeks() {
float total = 0; // declare a total variable of type float and initialize to 0 ( zero)
for (int w = 0; w < 5; w++)
{
total += getTotalSalesForWeek(w);
// sum up the total for the whole week and store it to the total variable
}
return total;
// return the sum computed above
}
float getAverageWeeklySales() {
return getTotalSalesForAllWeeks() / 5;
// AVERAGE for 5 days - just Monday to Friday only - excludes the week ends
}
int getWeekWithHighestSaleAmount() {
// top performing sales in the whole week
int maxWeek = 0;
float maxSale = -1;
for (int w = 0; w < 5; w++)
// run the for loop from 0 to 5 in steps of 1
{
float sale = getTotalSalesForWeek(w);
// first store the total sales in to the sale variable of type float
if (sale > maxSale)
{ // if at all if we find any amount greater than the max sale then replace max sale with the new sale amount
// and also note down the contributor - in the sense that which w ( week) achieved top sales
maxSale = sale;
maxWeek = w;
}
}
return maxWeek;
}
int getWeekWithLowestSaleAmount() {
int minWeek = 0;
float minSale = Float.MAX_VALUE;
for (int w = 0; w < 5; w++)
{
float sale = getTotalSalesForWeek(w);
if (sale < minSale)
{
minSale = sale;
minWeek = w;
}
}
// comments are same as the top sales except in reverse order
// first store an arbitary minimum sale figure
// then compare each running week's vaue with the lowest
// if at all when we encounter any value lower than the preset value then replace it
return minWeek;
// finally return the minimum value in that week
}
public void analyzeResults() {
for (int w = 0; w < 5; w++) // run the for loop from 0 to 5
{
System.out.printf("---- Week %d ----\n", w); // print a title decoration
System.out.printf(" Total sales: %.2f\n", getTotalSalesForWeek(w)); // display or print out the total sales summed earlier in called function
System.out.printf(" Average sales: %.2f\n", getAverageSalesForWeek(w)); // display the average sales figure
}
System.out.printf("\n");
System.out.printf("Total sales for all weeks: %.2f\n", getTotalSalesForAllWeeks()); // print sum of the sales for the entire week
System.out.printf("Average weekly sales: %.2f\n", getAverageWeeklySales()); // print weekly average sales
System.out.printf("Week with highest sale: %d\n", getWeekWithHighestSaleAmount()); // print highest performing or top sales
System.out.printf("Week with lowest sale: %d\n", getWeekWithLowestSaleAmount()); // print lowest sales or the struggling week
}
public void setsaleforweekdayintersection(int week, int day, float f) {
}
}
Franchise.java:
package store;
public class Franchise {
private Store stores[];
public Franchise(int num) { // now for a franchise store
stores = new Store[num]; // instantiate an array object of type class Store
// the class is Store
// the objects are named as stores
for(int i=0; i<=num; i++) stores[i] = new Store();
}
public Store getStores(int i) { // GETTER display or return values
return stores[i];
}
public void setStores(Store stores, int i) { // setter assign values
this.stores[i] = stores;
}
}
FileIO.java:
package store;
import java.io.*;
import java.util.StringTokenizer;
public class FileIO {
// Franchise readData(String filename)
Franchise readData(String filename, int numstores) {
Franchise f1 = new Franchise(numstores);
boolean DEBUG = true;
int ctr = 0;
// open the file
// read the line
// parse the line - get one value
// and set it in the correct location in 2 d array
try {
FileReader file = new FileReader(filename); // file is equivalent to a file pointer in c/c++
BufferedReader buff = new BufferedReader(file); // buffered reader will read a chunk in to the variable buff
boolean eof = false;
while (!eof) {
String line = buff.readLine();
ctr++;
if (line == null)
eof = true;
else {
if (DEBUG)
System.out.println(line);
if (ctr > 1) {
StringTokenizer a = new StringTokenizer(line);
for (int week = 0; week < 5; week++) {
for (int day = 0; day < 7; day++) {
String l = a.nextToken();
float f = Float.parseFloat(l); // parseFloat will store to variable f of type float
f1.getStores(ctr - 2)
.setsaleforweekdayintersection(week,
day, f);
if (DEBUG)
System.out.print("f" + f + " ");
}
}
}
}
}
} catch (IOException f2) {
}
return f1;
}
}
Driver.java:
package store;
public class Driver
{
public static void main(String[] args)
{
FileIO readdata = new FileIO();
Franchise f1 = readdata.readData("E:/Files/Salesdat.txt", 6);
System.out.println("Data read");
}
}
DriverImpl.java ( I got no idea why I need this subclass, but my tutor told me that I need this):
package store;
public class DriverImpl extends Driver {
}
I would like to change the line 10 in Franchise.java to
for(int i=0; i<num; i++) stores[i] = new Store();
Notice I removed the <= and put an = instead. Whenever dealing with array indices, one should always use the < comparator with the size as a good practice.
Valid indexes in an array are 0 to length - 1. Change <= to < like,
stores = new Store[num];
// the class is Store
// the objects are named as stores
for(int i=0; i<num; i++) stores[i] = new Store(); //stores[num] is invalid.

Beginner Java: How do I write a constructor to call methods?

I am trying to figure out how to write a constructor that calls methods. I have been given the following instructions for a Java project. The emboldened ones are relevant to this step. Step 3 I have completed, but I can't confirm if I completed it correctly. The code for Step 3 is the second Date constructor within the Date class.
Uncomment line 1 from DateTest (don’t forget to delete the “Line 1.” part) and build and run the project. What is the output? Why is this the output?
Create a default constructor for Date which sets the date to 1/1/2000. Build and run the project. What is the output?
Create a constructor that has three int parameters for the month, day, and year and sets the values of these instance variables to the values passed in. Uncomment lines 2 and 3. Build and run the project. What is the output?
Rewrite the constructor from question 3 so that it calls setMonth(), setDay(), and setYear(). Build and run the project. What is the output?
Write a set() method that has three parameters for the month, day, and year. Uncomment lines 4 and 5. Build and run the project. What is the output?
Rewrite the constructor from question 3 so that it calls set (). Build and run the project. What is the output?
Below is the code for Date class and DateTest class.
package datetest;
import java.util.Scanner;
public class Date
{
public Date() {
month = 1;
day = 1;
year = 2000;
}
public Date(int m, int d, int y) {
month = m;
day = d;
year = y;
}
private int month;
private int day;
private int year; //a four digit number.
public void setYear(int newYear)
{
year = newYear;
}
public void setMonth(int newMonth)
{
if ((newMonth <= 0) || (newMonth > 12))
{
month=newMonth;
}
else
month = newMonth;
}
public void setDay(int newDay)
{
if ((newDay <= 0) || (newDay > 31))
{
day=1;
}
else
day = newDay;
}
public int getMonth( )
{
return month;
}
public int getDay( )
{
return day;
}
public int getYear( )
{
return year;
}
public void printDate( )
{
System.out.print(getMonth() + "/" + getDay() + "/" + getYear());
}
public void readInput( )
{
boolean tryAgain = true;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter month, day, and year.");
System.out.println("Do not use a comma.");
month = keyboard.nextInt( );
day = keyboard.nextInt( );
year = keyboard.nextInt( );
}
}
This is the DateTest class.
package datetest;
public class DateTest {
public static void main(String[] args) {
Date today = new Date();
System.out.println("Today’s date is " + today.getMonth() + "/" + today.getDay() + "/" + today.getYear());
//Line 2. today = new Date(55, 55, 2011);
//Line 3. System.out.println("Today’s date is " + today.getMonth() + "/" + today.getDay() + "/" + today.getYear());
//Line 4. today.set(10, 5, 2011);
//Line 5. System.out.println("Today’s date is " + today.getMonth() + "/" + today.getDay() + "/" + today.getYear());
}
}
I have attempted to write the code to call the methods in step 4. Would the following code be the correct way to write a constructor to call methods?
public Date (int m, int d, int y) {
this.setMonth(month);
this.setDay(day);
this.setYear(year);
}
Would the following code be the correct way to write a constructor to call methods?
public Date (int m, int d, int y) {
this.setMonth(month);
this.setDay(day);
this.setYear(year);
}
Yes, if you used your m, d, and y arguments instead of month, day, and year:
public Date (int m, int d, int y) {
this.setMonth(m);
this.setDay(d);
this.setYear(y);
}
With your code, you're actually just setting the instance members (month and so on) to their existing values (because month in the constructor is automatically resolved to the instance data member month using an implied this.). So I'm guessing when you tried it, you ended up with zeroes and didn't understand why. (int members are auto-initialized to zero before the code in the constructor runs.)

Confusion about java dates comparison

Pre-notes:
Yes, this is homework.
Our school's tutor is out for the day
My book is useless
I'm not exactly sure what to search for on google for help with my confusion...
Question:
Anyway - The question / confusion that I have involves the first bit of code that I have in my program to test the compareTo method.
Would I use the variables at the top of the code as my variables in the static void main area, or assign new variables, like I do have?
The values in public Date()... <-- Is that the date that my code in static void main is comparing to? (If so, I have a piece of code that I want to use that uses the current date, rather than what's in Date()).
I may have more questions later on, but I hope that someone can clear up my confusion better than my book or google has proven thus far.
Code:
package date;
import java.util.*;
public class Date implements Comparable
{
static Scanner console = new Scanner(System.in);
private int dMonth; //Part a of confusion 1
private int dDay;
private int dYear;
public Date()
{
dMonth = 1; //Confusion 2
dDay = 1;
dYear = 1900;
}
public Date(int month, int day, int year)
{
dMonth = month;
dDay = day;
dYear = year;
}
public void setDate(int month, int day, int year)
{
dMonth = month;
dDay = day;
dYear = year;
}
public int getMonth()
{
return dMonth;
}
public int getDay()
{
return dDay;
}
public int getYear()
{
return dYear;
}
public String toString()
{
return (dMonth + "." + dDay + "." + dYear);
}
public boolean equals(Object otherDate)
{
Date temp = (Date) otherDate;
return (dYear == temp.dYear
&& dMonth == temp.dMonth
&& dDay == temp.dDay);
}
public int compareTo(Object otherDate)
{
Date temp = (Date) otherDate;
int yrDiff = dYear - temp.dYear;
if (yrDiff !=0)
return yrDiff;
int monthDiff = dMonth - temp.dMonth;
if (monthDiff !=0)
return monthDiff;
return dDay - temp.dDay;
}
public static void main(String[] args) //Part b of confusion 1
{
int month;
int day;
int year;
Date temp;
System.out.print("Enter date in the form of month day year");
month = console.nextInt();
day = console.nextInt();
year = console.nextInt();
System.out.println();
}
}
As mentioned in the comments, I think you need to read about the difference between static methods/attributes and the ones in instances. I think this is what you should be doing in the main method:
System.out.print("Enter date in the form of month day year");
Date date1 = new Date(console.nextInt(), console.nextInt(), console.nextInt());
System.out.print("Enter second date in the form of month day year");
Date date2 = new Date(console.nextInt(), console.nextInt(), console.nextInt());
System.out.println("Comparison result:");
System.out.println(date1.compareTo(date2));
Regarding your confusion points:
Class attributes
private int dMonth; //Part a of confusion 1
private int dDay;
private int dYear;
These are special variables. Each instance (that is, every object created with new Date) has its own value for dMonth, dDay and dYear. It is not accessible from the main because main is a static method, and thus doesn't have access to instance variables.
If you didn't understand, at least you know the names to search further.
Default constructor
public Date()
{
dMonth = 1; //Confusion 2
dDay = 1;
dYear = 1900;
}
Those values are used when you create a new Date object without specifying which month/day/year you want. So new Date(2, 3, 2013) means 2/3/2013, while new Date() means 1/1/1900.
No you can't, dMonth, dDay and dYear are member variables. If you want to use them directly inside your main method you will have to use the keyword static so that they become class variables. But no, that is not what you want.
Your main method is doing nothing useful really. Your confusion point 2 is a constructor:
Date d = new Date(); // Data Instance -> First constructor
d.getMonth(); // 1
d.getDay(); // 1
d.getYear(); // 1900
Date d2 = new Date(2, 2, 1901);
d2.getMonth(); // 2
d2.getDay(); // 2
d2.getYear(); // 1901
d2.setDate(3, 3, 1902);
d2.getMonth(); // 3
d2.getDay(); // 3
d2.getYear(); // 1902
d.getMonth(); // Still 1 since member variables of d are independent of d2
d.compareTo(d2); // -2 -> (1900 - 1902)
You can create date instances inside your main method and use code like the one above to access member variables (probably the whole point of your exercise).

Compare two objects, and return string. But one object doesn't take parameters?

This is homework.
GOAL: I want to compare the date of two objects to decide whether my person object is an adult or not and store this in a string.
The strange thing is, all my values of date d1 are 0;
public class Date {
public int day, month, year;
public String child
Date(date d1, date d2) {
if ((d1.year - d2.year > 18) ||
((d1.year - d2.year == 18) && (d2.year> d1.year)) ||
((d1.year - d2.year == 18) && (d2.year == d1.maand) && (d2.day > d1.day))) {
child = adult;
} else {
child = child;
}
Date(int a, int b, int c) {
a = year;
b = month;
c = day;
}
Date (String birthdate) {
String pattern = "\\d{2}-\\d{2}-\\d{4}";
boolean b = birthdate.matches(pattern);
if (b) {
String[] str = birthdate.split("-");
for (String s: str)
this.day = Integer.parseInt(str[0]);
this.month = Integer.parseInt(str[1]);
this.year = Integer.parseInt(str[2]);
this.child = false;
} else {
System.out.println("Wrong format");
}
}
When I make a test, this happens:
System.out.println("D1 year = " + d1.year);
System.out.println("D1 day = " + d1.day);
System.out.println("D1 month = " + d1.month);
Result:
D1 year = 0
D1 day = 0
D1 month = 0
Why does this happen? Lets look at my other class.
My other class, where my method infoPerson is located is as following:
public static Person infoPerson() {
String name, lastname, birthdate;
Datum birthday, today;
System.out.println("Firstname:");
name = userInput();
System.out.println("Lastname:");
lastname = userInput();
System.out.println("Birthdate?:");
birthdate = userInput();
//here I send the string birthdate to my Date class
birthday = new Date(birthdate);
today = new Date(3, 7, 2013);
//Here I want to compare my two Date objects, today and birthday. This is were I got stuck, how do I do this correctly?
dateChild = new Date(today, birthday);
// here i send the new date to my Person class what consists of two strings and Data birthday
return new Gast(name, lastname, dateChild);
}
The assignment in the constructor is reversed:
Date(int a, int b, int c) {
a = year; // should be year = a;
b = month; // month = b;
c = day; // day = c;
}
Please don't use the class name same as the one defined in Java API. Date is already a class in java.util package.
Apart from that there are many compiler errors in your code:
public string child - isn't going to compile. Should be String not string.
void compareTo(date d1, date d2) - I don't know what you're trying to do here. But this too won't compile. Undefined type - date
You've declared the Datum birthday and initializing it using new Date(...). That too wouldn't work.
For some reason, I feel like you don't have any method in your class, but just a bunch of constructors. My suggestion would be - throw that code away, and start afresh.
And please don't use a bunch of integer fields to store birthdays. Use a Calendar instance instead.

Categories

Resources