SlideShare a Scribd company logo
1 of 20
Download to read offline
java programming 5th edition chapter 8 User-Defined Classes and ADTs usingStrings no Arrays
This is what the output is supposed to look like:
Output for day1:
The current day is
Output for day2:
The current day is Tuesday
New output for day1:
The current day is Friday
Output for day2: Tuesday
The day after Tuesday is Wednesday
The day before Tuesday is Monday
Enter the number of days from Friday: 3
A Monday is 3 days past Friday
Enter the number of days from Tuesday: 12
A Sunday is 12 days past Tuesday
This is the First program I had:
public class Day
{
private String day; //day of the week being converted to a number
private int convertNum; // the number being converted to a day of the week My teacher said to
take this one out because there is only one attribute which is day
//default constructor
public Day()
{
day = "";
convertNum = 0;
}//end of default constructor
//values constructor
public Day (String weekDay, int dayNum)
{
day = weekDay;
convertNum = dayNum;
}//end of values constructor
//set methods
public void setConvertDayToNum(String weekDay)
{
day = weekDay;
convertNum = getConvertDayToNum(day);
}
public void setConvertNumToDay(int dayNum)
{
convertNum = dayNum;
day = getConvertNumToDay(convertNum);
}
//Where we convert a number to a day of the week
public String getConvertNumToDay(int convertNum)
{
String day1 = "";
switch (convertNum)
{
case 1:
day1 = "Sunday";
break;
case 2:
day1 = "Monday";
break;
case 3:
day1 = "Tuesday";
break;
case 4:
day1 = "Wednesday";
break;
case 5:
day1 = "Thurday";
break;
case 6:
day1 = "Friday";
break;
case 7:
day1 = "Saturday";
break;
}// switch(convertNum)
return day1;
}//end of getDay
//Where we convert the day of the week to a number
public int getConvertDayToNum(String day)
{
int conversion = 0;
switch (day)
{
case "Sunday":
conversion = 1;
break;
case "Monday":
conversion = 2;
break;
case "Tuesday":
conversion = 3;
break;
case "Wednesday":
conversion = 4;
break;
case "Thursday":
conversion = 5;
break;
case "Friday":
conversion = 6;
break;
case "Saturday":
conversion = 7;
break;
}//end switch(day)
return conversion;
}//end of getNewDay
public void calcPrevDay()
{
String day1;
int conversion;
conversion = convertNum - 1;
if (conversion == 0)
{
conversion = 7;
}
day1 = getConvertNumToDay(conversion);
System.out.print("The day before " + day + " is " + day1);
}//end calcPrevDay()
public void calcNextDay()
{
String day1;
int conversion;
conversion = convertNum + 1;
if (conversion == 8)
{
conversion = 1;
}
day1 = getConvertNumToDay(conversion);
System.out.print(" The day after " + day + " is " + day1);
}//end calcNextDay()
public void calcNewDay(int addOnDays)
{
String newDay = "";
int totDaysNum = 0;
totDaysNum = convertNum + addOnDays % 7;
if (totDaysNum > 7)
{
totDaysNum = totDaysNum % 7;
}
newDay = getConvertNumToDay(totDaysNum);
System.out.print("A " + newDay + " is " + addOnDays + " days past " + day + " ");
}//end calcNewDay
public void printDay()
{
System.out.print("The Current Day is " + day + " ");
}//end printDay()
}//end of class Day
import java.util.*;
public class testDay
{
static Scanner console = new Scanner (System.in);
public static void main (String[]args)
{
//declare local variable
String day = "";
int convertNum = 0;
int addOnDays = 0;
Day day1 = new Day();
Day day2 = new Day("Tuesday", 3);
System.out.print("Output for day1:  ");
day1.printDay();
System.out.println();
System.out.print("Output for day2:  ");
convertNum = day2.getConvertDayToNum(day);
day2.printDay();
System.out.print(" New output for day1:  ");
day1.setConvertDayToNum(day);
day1.printDay();
System.out.print(" Output for day2: ");
day = console.nextLine();
day2.calcPrevDay();
day2.calcNextDay();
System.out.print("  Enter the number of days from Friday: ");
addOnDays = console.nextInt();
day1.calcNewDay(addOnDays);
System.out.print("  Enter the number of days from Tuesday: ");
addOnDays = console.nextInt();
day2.calcNewDay(addOnDays);
}//end main
}//end class testDay
This was my output for that one:
Output for day1:
The Current Day is
Output for day2:
The Current Day is Tuesday
New output for day1:
The Current Day is
Output for day2: Tuesday
The day before Tuesday is Monday
The day after Tuesday is Wednesday
Enter the number of days from Friday: 3
A Tuesday is 3 days past
Enter the number of days from Tuesday: 12
A Sunday is 12 days past Tuesday
----jGRASP: operation complete.
----jGRASP exec: java testDay
Output for day1:
The Current Day is
Output for day2:
The Current Day is Tuesday
New output for day1:
The Current Day is
Output for day2: Sunday
The day before Tuesday is Monday
The day after Tuesday is Wednesday
Enter the number of days from Friday: 3
A Tuesday is 3 days past
Enter the number of days from Tuesday: 12
A Sunday is 12 days past Tuesday
I did what the teacher said and took out the other attribute:
public class Day2
{
private String day; //day of the week being converted to a number
//default constructor
public Day2()
{
day = "";
}//end of default constructor
//values constructor
public Day2 (String weekDay)
{
day = weekDay;
}//end of values constructor
//set methods
public void setConvertDayToNum(String weekDay)
{
int convertNum;
day = weekDay;
convertNum = getConvertDayToNum(day);
}
public void setConvertNumToDay(int dayNum)
{
int convertNum = 0;
convertNum = dayNum;
day = getConvertNumToDay(convertNum);
}
//Where we convert a number to a day of the week
public String getConvertNumToDay(int convertNum)
{
String day1 = "";
switch (convertNum)
{
case 1:
day1 = "Sunday";
break;
case 2:
day1 = "Monday";
break;
case 3:
day1 = "Tuesday";
break;
case 4:
day1 = "Wednesday";
break;
case 5:
day1 = "Thurday";
break;
case 6:
day1 = "Friday";
break;
case 7:
day1 = "Saturday";
break;
}// switch(convertNum)
return day1;
}//end of getDay
//Where we convert the day of the week to a number
public int getConvertDayToNum(String day)
{
int conversion = 0;
switch (day)
{
case "Sunday":
conversion = 1;
break;
case "Monday":
conversion = 2;
break;
case "Tuesday":
conversion = 3;
break;
case "Wednesday":
conversion = 4;
break;
case "Thursday":
conversion = 5;
break;
case "Friday":
conversion = 6;
break;
case "Saturday":
conversion = 7;
break;
}//end switch(day)
return conversion;
}//end of getNewDay
public void calcPrevDay()
{ int convertNum = 0;
String day1;
int conversion;
conversion = convertNum - 1;
if (conversion == 0)
{
conversion = 7;
}
day1 = getConvertNumToDay(conversion);
System.out.print("The day before " + day + " is " + day1);
}//end calcPrevDay()
public void calcNextDay()
{
int convertNum = 0;
String day1;
int conversion;
conversion = convertNum + 1;
if (conversion == 8)
{
conversion = 1;
}
day1 = getConvertNumToDay(conversion);
System.out.print(" The day after " + day + " is " + day1);
}//end calcNextDay()
public void calcNewDay(int addOnDays)
{
int convertNum = 0;
String newDay = "";
int totDaysNum = 0;
totDaysNum = convertNum + addOnDays % 7;
if (totDaysNum > 7)
{
totDaysNum = totDaysNum % 7;
}
newDay = getConvertNumToDay(totDaysNum);
System.out.print("A " + newDay + " is " + addOnDays + " days past " + day + " ");
}//end calcNewDay
public void printDay()
{
System.out.print("The Current Day is " + day + " ");
}//end printDay()
}//end of class Day
import java.util.*;
public class testDay2
{
static Scanner console = new Scanner (System.in);
public static void main (String[]args)
{
//declare local variable
String day = "";
int convertNum = 0;
int addOnDays = 0;
Day2 day1 = new Day2();
Day2 day2 = new Day2("Tuesday");
System.out.print("Output for day1:  ");
day1.printDay();
System.out.println();
System.out.print("Output for day2:  ");
convertNum = day2.getConvertDayToNum(day);
day2.printDay();
System.out.print(" New output for day1:  ");
day1.setConvertDayToNum(day);
day1.printDay();
System.out.print(" Output for day2: ");
day = console.nextLine();
day2.calcPrevDay();
day2.calcNextDay();
System.out.print("  Enter the number of days from Friday: ");
addOnDays = console.nextInt();
day1.calcNewDay(addOnDays);
System.out.print("  Enter the number of days from Tuesday: ");
addOnDays = console.nextInt();
day2.calcNewDay(addOnDays);
}//end main
}//end class testDay
Then this was my output:
Output for day1:
The Current Day is
Output for day2:
The Current Day is Tuesday
New output for day1:
The Current Day is
Output for day2: Tuesday
The day before Tuesday is
The day after Tuesday is Sunday
Enter the number of days from Friday: 3
A Tuesday is 3 days past
Enter the number of days from Tuesday: 12
A Thurday is 12 days past Tuesday
----jGRASP: operation complete.
----jGRASP exec: java testDay2
Output for day1:
The Current Day is
Output for day2:
The Current Day is Tuesday
New output for day1:
The Current Day is
Output for day2: Wednesday
The day before Tuesday is
The day after Tuesday is Sunday
Enter the number of days from Friday: 3
A Tuesday is 3 days past
Enter the number of days from Tuesday: 12
A Thurday is 12 days past Tuesday
----jGRASP: operation complete.
I have spent so much time on this please help me fix it
Solution
PROGRAM CODE:
Day2.java
package array;
public class Day2
{
private String day; //day of the week being converted to a number
//default constructor
public Day2()
{
day = "";
}//end of default constructor
//values constructor
public Day2 (String weekDay)
{
day = weekDay;
}//end of values constructor
//set methods
public void setConvertDayToNum(String weekDay)
{
int convertNum;
day = weekDay;
convertNum = getConvertDayToNum(day);
}
public void setConvertNumToDay(int dayNum)
{
int convertNum = 0;
convertNum = dayNum;
day = getConvertNumToDay(convertNum);
}
//Where we convert a number to a day of the week
public String getConvertNumToDay(int convertNum)
{
String day1 = "";
switch (convertNum)
{
case 1:
day1 = "Sunday";
break;
case 2:
day1 = "Monday";
break;
case 3:
day1 = "Tuesday";
break;
case 4:
day1 = "Wednesday";
break;
case 5:
day1 = "Thursday";
break;
case 6:
day1 = "Friday";
break;
case 7:
day1 = "Saturday";
break;
}// switch(convertNum)
return day1;
}//end of getDay
//Where we convert the day of the week to a number
public int getConvertDayToNum(String day)
{
int conversion = 0;
switch (day)
{
case "Sunday":
conversion = 1;
break;
case "Monday":
conversion = 2;
break;
case "Tuesday":
conversion = 3;
break;
case "Wednesday":
conversion = 4;
break;
case "Thursday":
conversion = 5;
break;
case "Friday":
conversion = 6;
break;
case "Saturday":
conversion = 7;
break;
}//end switch(day)
return conversion;
}//end of getNewDay
public void calcPrevDay()
//Made changes here to calculate the num from the day
{ int convertNum = getConvertDayToNum(day);
String day1;
int conversion;
conversion = convertNum - 1;
if (conversion == 0)
{
conversion = 7;
}
day1 = getConvertNumToDay(conversion);
System.out.print(" The day before " + day + " is " + day1);
}//end calcPrevDay()
public void calcNextDay()
{
//Made changes here to calculate the num from the day
int convertNum = getConvertDayToNum(day);
String day1;
int conversion;
conversion = convertNum + 1;
if (conversion == 8)
{
conversion = 1;
}
day1 = getConvertNumToDay(conversion);
System.out.print(" The day after " + day + " is " + day1);
}//end calcNextDay()
public void calcNewDay(int addOnDays)
{
//Made changes here to calculate the num from the day
int convertNum = getConvertDayToNum(day);;
String newDay = "";
int totDaysNum = 0;
totDaysNum = convertNum + addOnDays % 7;
if (totDaysNum > 7)
{
totDaysNum = totDaysNum % 7;
}
newDay = getConvertNumToDay(totDaysNum);
System.out.print("A " + newDay + " is " + addOnDays + " days past " + day + " ");
}//end calcNewDay
public void printDay()
{
System.out.print("The Current Day is " + day + " ");
}//end printDay()
}//end of class Day
testDay2.java
package array;
import java.util.*;
public class testDay2
{
static Scanner console = new Scanner (System.in);
public static void main (String[]args)
{
//declare local variable
String day = "";
int convertNum = 0;
int addOnDays = 0;
Day2 day1 = new Day2();
Day2 day2 = new Day2("Tuesday");
System.out.print("Output for day1:  ");
day1.printDay();
System.out.println();
System.out.print("Output for day2:  ");
convertNum = day2.getConvertDayToNum(day);
day2.printDay();
System.out.print(" New output for day1:  ");
//Made changed here to convert from num to day since day to num is now invalid
day1.setConvertNumToDay(6);
day1.printDay();
System.out.print(" Output for day2: ");
day = console.nextLine();
day2.calcNextDay();
day2.calcPrevDay();
System.out.print("  Enter the number of days from Friday: ");
addOnDays = console.nextInt();
day1.calcNewDay(addOnDays);
System.out.print("  Enter the number of days from Tuesday: ");
addOnDays = console.nextInt();
day2.calcNewDay(addOnDays);
}//end main
}//end class testDay
OUTPUT:
Output for day1:
The Current Day is
Output for day2:
The Current Day is Tuesday
New output for day1:
The Current Day is Friday
Output for day2: Tuesday
The day after Tuesday is Wednesday
The day before Tuesday is Monday
Enter the number of days from Friday: 3
A Monday is 3 days past Friday
Enter the number of days from Tuesday: 12
A Sunday is 12 days past Tuesday

More Related Content

Similar to java programming 5th edition chapter 8 User-Defined Classes and ADTs.pdf

WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_10-Feb-2021_L4_...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_10-Feb-2021_L4_...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_10-Feb-2021_L4_...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_10-Feb-2021_L4_...
MaruMengesha
 

Similar to java programming 5th edition chapter 8 User-Defined Classes and ADTs.pdf (8)

ThreeTen
ThreeTenThreeTen
ThreeTen
 
Refactoring
RefactoringRefactoring
Refactoring
 
GPA calculator and grading program in c++
GPA calculator and grading program in c++GPA calculator and grading program in c++
GPA calculator and grading program in c++
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_10-Feb-2021_L4_...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_10-Feb-2021_L4_...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_10-Feb-2021_L4_...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_10-Feb-2021_L4_...
 
this is java. problem.i already make employee class. but i can not.pdf
this is java. problem.i already make employee class. but i can not.pdfthis is java. problem.i already make employee class. but i can not.pdf
this is java. problem.i already make employee class. but i can not.pdf
 
Scalable JavaScript
Scalable JavaScriptScalable JavaScript
Scalable JavaScript
 
data structures and algorithms Unit 4
data structures and algorithms Unit 4data structures and algorithms Unit 4
data structures and algorithms Unit 4
 
Making a Process
Making a ProcessMaking a Process
Making a Process
 

More from fazalenterprises

Brett Donovan was the manager at Waltons Diner. He planned to promo.pdf
Brett Donovan was the manager at Waltons Diner. He planned to promo.pdfBrett Donovan was the manager at Waltons Diner. He planned to promo.pdf
Brett Donovan was the manager at Waltons Diner. He planned to promo.pdf
fazalenterprises
 
C# using Visual studio - Windows Form. If possible step-by-step inst.pdf
C# using Visual studio - Windows Form. If possible step-by-step inst.pdfC# using Visual studio - Windows Form. If possible step-by-step inst.pdf
C# using Visual studio - Windows Form. If possible step-by-step inst.pdf
fazalenterprises
 
A number of philosophers held various versions of the view that lang.pdf
A number of philosophers held various versions of the view that lang.pdfA number of philosophers held various versions of the view that lang.pdf
A number of philosophers held various versions of the view that lang.pdf
fazalenterprises
 
Why phelogyny has to be this way not other way aroundExaplain bas.pdf
Why phelogyny has to be this way not other way aroundExaplain bas.pdfWhy phelogyny has to be this way not other way aroundExaplain bas.pdf
Why phelogyny has to be this way not other way aroundExaplain bas.pdf
fazalenterprises
 
What is John Tukeys relationship to the S language and to the R la.pdf
What is John Tukeys relationship to the S language and to the R la.pdfWhat is John Tukeys relationship to the S language and to the R la.pdf
What is John Tukeys relationship to the S language and to the R la.pdf
fazalenterprises
 
What do health care professionals need to remember when caring for p.pdf
What do health care professionals need to remember when caring for p.pdfWhat do health care professionals need to remember when caring for p.pdf
What do health care professionals need to remember when caring for p.pdf
fazalenterprises
 
The problem is that I have to jump to the administrative function u.pdf
The problem is that I have to jump to the administrative function u.pdfThe problem is that I have to jump to the administrative function u.pdf
The problem is that I have to jump to the administrative function u.pdf
fazalenterprises
 
The FASB has developed specific guidelines for what to include in in.pdf
The FASB has developed specific guidelines for what to include in in.pdfThe FASB has developed specific guidelines for what to include in in.pdf
The FASB has developed specific guidelines for what to include in in.pdf
fazalenterprises
 
Read and reflect on the case study involving the CEO of Xerox on pag.pdf
Read and reflect on the case study involving the CEO of Xerox on pag.pdfRead and reflect on the case study involving the CEO of Xerox on pag.pdf
Read and reflect on the case study involving the CEO of Xerox on pag.pdf
fazalenterprises
 

More from fazalenterprises (20)

Brett Donovan was the manager at Waltons Diner. He planned to promo.pdf
Brett Donovan was the manager at Waltons Diner. He planned to promo.pdfBrett Donovan was the manager at Waltons Diner. He planned to promo.pdf
Brett Donovan was the manager at Waltons Diner. He planned to promo.pdf
 
Being larger than the right ventricle, the left ventricle pumps more .pdf
Being larger than the right ventricle, the left ventricle pumps more .pdfBeing larger than the right ventricle, the left ventricle pumps more .pdf
Being larger than the right ventricle, the left ventricle pumps more .pdf
 
C# using Visual studio - Windows Form. If possible step-by-step inst.pdf
C# using Visual studio - Windows Form. If possible step-by-step inst.pdfC# using Visual studio - Windows Form. If possible step-by-step inst.pdf
C# using Visual studio - Windows Form. If possible step-by-step inst.pdf
 
A number of philosophers held various versions of the view that lang.pdf
A number of philosophers held various versions of the view that lang.pdfA number of philosophers held various versions of the view that lang.pdf
A number of philosophers held various versions of the view that lang.pdf
 
Why phelogyny has to be this way not other way aroundExaplain bas.pdf
Why phelogyny has to be this way not other way aroundExaplain bas.pdfWhy phelogyny has to be this way not other way aroundExaplain bas.pdf
Why phelogyny has to be this way not other way aroundExaplain bas.pdf
 
Which of the following are tertiary activities A. Lead mining.pdf
Which of the following are tertiary activities A. Lead mining.pdfWhich of the following are tertiary activities A. Lead mining.pdf
Which of the following are tertiary activities A. Lead mining.pdf
 
What is John Tukeys relationship to the S language and to the R la.pdf
What is John Tukeys relationship to the S language and to the R la.pdfWhat is John Tukeys relationship to the S language and to the R la.pdf
What is John Tukeys relationship to the S language and to the R la.pdf
 
What do health care professionals need to remember when caring for p.pdf
What do health care professionals need to remember when caring for p.pdfWhat do health care professionals need to remember when caring for p.pdf
What do health care professionals need to remember when caring for p.pdf
 
What is the difference between a neurogenic and myogenic heart Plea.pdf
What is the difference between a neurogenic and myogenic heart Plea.pdfWhat is the difference between a neurogenic and myogenic heart Plea.pdf
What is the difference between a neurogenic and myogenic heart Plea.pdf
 
This is sociology. Content Assessments Communications Resources He.pdf
This is sociology. Content Assessments Communications Resources He.pdfThis is sociology. Content Assessments Communications Resources He.pdf
This is sociology. Content Assessments Communications Resources He.pdf
 
There is an area that has been classified, according to the IEC requ.pdf
There is an area that has been classified, according to the IEC requ.pdfThere is an area that has been classified, according to the IEC requ.pdf
There is an area that has been classified, according to the IEC requ.pdf
 
The problem is that I have to jump to the administrative function u.pdf
The problem is that I have to jump to the administrative function u.pdfThe problem is that I have to jump to the administrative function u.pdf
The problem is that I have to jump to the administrative function u.pdf
 
The IRS has the right to revoke an installment agreement for any of .pdf
The IRS has the right to revoke an installment agreement for any of .pdfThe IRS has the right to revoke an installment agreement for any of .pdf
The IRS has the right to revoke an installment agreement for any of .pdf
 
The FASB has developed specific guidelines for what to include in in.pdf
The FASB has developed specific guidelines for what to include in in.pdfThe FASB has developed specific guidelines for what to include in in.pdf
The FASB has developed specific guidelines for what to include in in.pdf
 
A difference between bacterial and eukaryotic transcriptionMultipl.pdf
A difference between bacterial and eukaryotic transcriptionMultipl.pdfA difference between bacterial and eukaryotic transcriptionMultipl.pdf
A difference between bacterial and eukaryotic transcriptionMultipl.pdf
 
Ten independent observations were made of the time to load a pallet..pdf
Ten independent observations were made of the time to load a pallet..pdfTen independent observations were made of the time to load a pallet..pdf
Ten independent observations were made of the time to load a pallet..pdf
 
Read and reflect on the case study involving the CEO of Xerox on pag.pdf
Read and reflect on the case study involving the CEO of Xerox on pag.pdfRead and reflect on the case study involving the CEO of Xerox on pag.pdf
Read and reflect on the case study involving the CEO of Xerox on pag.pdf
 
QUESTION 2 The current account is the record of O a. foreign investme.pdf
QUESTION 2 The current account is the record of O a. foreign investme.pdfQUESTION 2 The current account is the record of O a. foreign investme.pdf
QUESTION 2 The current account is the record of O a. foreign investme.pdf
 
MULTIPLE CHOICESSolution5. Correct Answer B) Self Servicing.pdf
MULTIPLE CHOICESSolution5. Correct Answer B) Self Servicing.pdfMULTIPLE CHOICESSolution5. Correct Answer B) Self Servicing.pdf
MULTIPLE CHOICESSolution5. Correct Answer B) Self Servicing.pdf
 
Mendel’s law of dominance supports thatOne person dominates over .pdf
Mendel’s law of dominance supports thatOne person dominates over .pdfMendel’s law of dominance supports thatOne person dominates over .pdf
Mendel’s law of dominance supports thatOne person dominates over .pdf
 

Recently uploaded

Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
中 央社
 

Recently uploaded (20)

ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptx
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
Climbers and Creepers used in landscaping
Climbers and Creepers used in landscapingClimbers and Creepers used in landscaping
Climbers and Creepers used in landscaping
 
Scopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS PublicationsScopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS Publications
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 

java programming 5th edition chapter 8 User-Defined Classes and ADTs.pdf

  • 1. java programming 5th edition chapter 8 User-Defined Classes and ADTs usingStrings no Arrays This is what the output is supposed to look like: Output for day1: The current day is Output for day2: The current day is Tuesday New output for day1: The current day is Friday Output for day2: Tuesday The day after Tuesday is Wednesday The day before Tuesday is Monday Enter the number of days from Friday: 3 A Monday is 3 days past Friday Enter the number of days from Tuesday: 12 A Sunday is 12 days past Tuesday This is the First program I had: public class Day { private String day; //day of the week being converted to a number private int convertNum; // the number being converted to a day of the week My teacher said to take this one out because there is only one attribute which is day //default constructor public Day() { day = ""; convertNum = 0; }//end of default constructor //values constructor public Day (String weekDay, int dayNum) { day = weekDay; convertNum = dayNum; }//end of values constructor
  • 2. //set methods public void setConvertDayToNum(String weekDay) { day = weekDay; convertNum = getConvertDayToNum(day); } public void setConvertNumToDay(int dayNum) { convertNum = dayNum; day = getConvertNumToDay(convertNum); } //Where we convert a number to a day of the week public String getConvertNumToDay(int convertNum) { String day1 = ""; switch (convertNum) { case 1: day1 = "Sunday"; break; case 2: day1 = "Monday"; break; case 3: day1 = "Tuesday"; break; case 4: day1 = "Wednesday"; break; case 5: day1 = "Thurday"; break;
  • 3. case 6: day1 = "Friday"; break; case 7: day1 = "Saturday"; break; }// switch(convertNum) return day1; }//end of getDay //Where we convert the day of the week to a number public int getConvertDayToNum(String day) { int conversion = 0; switch (day) { case "Sunday": conversion = 1; break; case "Monday": conversion = 2; break; case "Tuesday": conversion = 3; break; case "Wednesday": conversion = 4; break; case "Thursday": conversion = 5; break; case "Friday": conversion = 6;
  • 4. break; case "Saturday": conversion = 7; break; }//end switch(day) return conversion; }//end of getNewDay public void calcPrevDay() { String day1; int conversion; conversion = convertNum - 1; if (conversion == 0) { conversion = 7; } day1 = getConvertNumToDay(conversion); System.out.print("The day before " + day + " is " + day1); }//end calcPrevDay() public void calcNextDay() { String day1; int conversion; conversion = convertNum + 1; if (conversion == 8) { conversion = 1; }
  • 5. day1 = getConvertNumToDay(conversion); System.out.print(" The day after " + day + " is " + day1); }//end calcNextDay() public void calcNewDay(int addOnDays) { String newDay = ""; int totDaysNum = 0; totDaysNum = convertNum + addOnDays % 7; if (totDaysNum > 7) { totDaysNum = totDaysNum % 7; } newDay = getConvertNumToDay(totDaysNum); System.out.print("A " + newDay + " is " + addOnDays + " days past " + day + " "); }//end calcNewDay public void printDay() { System.out.print("The Current Day is " + day + " "); }//end printDay() }//end of class Day import java.util.*; public class testDay { static Scanner console = new Scanner (System.in); public static void main (String[]args) { //declare local variable String day = "";
  • 6. int convertNum = 0; int addOnDays = 0; Day day1 = new Day(); Day day2 = new Day("Tuesday", 3); System.out.print("Output for day1: "); day1.printDay(); System.out.println(); System.out.print("Output for day2: "); convertNum = day2.getConvertDayToNum(day); day2.printDay(); System.out.print(" New output for day1: "); day1.setConvertDayToNum(day); day1.printDay(); System.out.print(" Output for day2: "); day = console.nextLine(); day2.calcPrevDay(); day2.calcNextDay(); System.out.print(" Enter the number of days from Friday: "); addOnDays = console.nextInt(); day1.calcNewDay(addOnDays); System.out.print(" Enter the number of days from Tuesday: "); addOnDays = console.nextInt(); day2.calcNewDay(addOnDays); }//end main }//end class testDay This was my output for that one: Output for day1:
  • 7. The Current Day is Output for day2: The Current Day is Tuesday New output for day1: The Current Day is Output for day2: Tuesday The day before Tuesday is Monday The day after Tuesday is Wednesday Enter the number of days from Friday: 3 A Tuesday is 3 days past Enter the number of days from Tuesday: 12 A Sunday is 12 days past Tuesday ----jGRASP: operation complete. ----jGRASP exec: java testDay Output for day1: The Current Day is Output for day2: The Current Day is Tuesday New output for day1: The Current Day is Output for day2: Sunday The day before Tuesday is Monday The day after Tuesday is Wednesday Enter the number of days from Friday: 3 A Tuesday is 3 days past Enter the number of days from Tuesday: 12 A Sunday is 12 days past Tuesday I did what the teacher said and took out the other attribute: public class Day2 { private String day; //day of the week being converted to a number //default constructor public Day2()
  • 8. { day = ""; }//end of default constructor //values constructor public Day2 (String weekDay) { day = weekDay; }//end of values constructor //set methods public void setConvertDayToNum(String weekDay) { int convertNum; day = weekDay; convertNum = getConvertDayToNum(day); } public void setConvertNumToDay(int dayNum) { int convertNum = 0; convertNum = dayNum; day = getConvertNumToDay(convertNum); } //Where we convert a number to a day of the week public String getConvertNumToDay(int convertNum) { String day1 = ""; switch (convertNum) {
  • 9. case 1: day1 = "Sunday"; break; case 2: day1 = "Monday"; break; case 3: day1 = "Tuesday"; break; case 4: day1 = "Wednesday"; break; case 5: day1 = "Thurday"; break; case 6: day1 = "Friday"; break; case 7: day1 = "Saturday"; break; }// switch(convertNum) return day1; }//end of getDay //Where we convert the day of the week to a number public int getConvertDayToNum(String day) { int conversion = 0; switch (day) { case "Sunday": conversion = 1;
  • 10. break; case "Monday": conversion = 2; break; case "Tuesday": conversion = 3; break; case "Wednesday": conversion = 4; break; case "Thursday": conversion = 5; break; case "Friday": conversion = 6; break; case "Saturday": conversion = 7; break; }//end switch(day) return conversion; }//end of getNewDay public void calcPrevDay() { int convertNum = 0; String day1; int conversion; conversion = convertNum - 1; if (conversion == 0) { conversion = 7; }
  • 11. day1 = getConvertNumToDay(conversion); System.out.print("The day before " + day + " is " + day1); }//end calcPrevDay() public void calcNextDay() { int convertNum = 0; String day1; int conversion; conversion = convertNum + 1; if (conversion == 8) { conversion = 1; } day1 = getConvertNumToDay(conversion); System.out.print(" The day after " + day + " is " + day1); }//end calcNextDay() public void calcNewDay(int addOnDays) { int convertNum = 0; String newDay = ""; int totDaysNum = 0; totDaysNum = convertNum + addOnDays % 7; if (totDaysNum > 7) { totDaysNum = totDaysNum % 7; } newDay = getConvertNumToDay(totDaysNum); System.out.print("A " + newDay + " is " + addOnDays + " days past " + day + " ");
  • 12. }//end calcNewDay public void printDay() { System.out.print("The Current Day is " + day + " "); }//end printDay() }//end of class Day import java.util.*; public class testDay2 { static Scanner console = new Scanner (System.in); public static void main (String[]args) { //declare local variable String day = ""; int convertNum = 0; int addOnDays = 0; Day2 day1 = new Day2(); Day2 day2 = new Day2("Tuesday"); System.out.print("Output for day1: "); day1.printDay(); System.out.println(); System.out.print("Output for day2: "); convertNum = day2.getConvertDayToNum(day); day2.printDay(); System.out.print(" New output for day1: "); day1.setConvertDayToNum(day); day1.printDay(); System.out.print(" Output for day2: ");
  • 13. day = console.nextLine(); day2.calcPrevDay(); day2.calcNextDay(); System.out.print(" Enter the number of days from Friday: "); addOnDays = console.nextInt(); day1.calcNewDay(addOnDays); System.out.print(" Enter the number of days from Tuesday: "); addOnDays = console.nextInt(); day2.calcNewDay(addOnDays); }//end main }//end class testDay Then this was my output: Output for day1: The Current Day is Output for day2: The Current Day is Tuesday New output for day1: The Current Day is Output for day2: Tuesday The day before Tuesday is The day after Tuesday is Sunday Enter the number of days from Friday: 3 A Tuesday is 3 days past Enter the number of days from Tuesday: 12 A Thurday is 12 days past Tuesday ----jGRASP: operation complete. ----jGRASP exec: java testDay2 Output for day1: The Current Day is Output for day2: The Current Day is Tuesday
  • 14. New output for day1: The Current Day is Output for day2: Wednesday The day before Tuesday is The day after Tuesday is Sunday Enter the number of days from Friday: 3 A Tuesday is 3 days past Enter the number of days from Tuesday: 12 A Thurday is 12 days past Tuesday ----jGRASP: operation complete. I have spent so much time on this please help me fix it Solution PROGRAM CODE: Day2.java package array; public class Day2 { private String day; //day of the week being converted to a number //default constructor public Day2() { day = ""; }//end of default constructor //values constructor public Day2 (String weekDay) { day = weekDay; }//end of values constructor //set methods public void setConvertDayToNum(String weekDay) { int convertNum;
  • 15. day = weekDay; convertNum = getConvertDayToNum(day); } public void setConvertNumToDay(int dayNum) { int convertNum = 0; convertNum = dayNum; day = getConvertNumToDay(convertNum); } //Where we convert a number to a day of the week public String getConvertNumToDay(int convertNum) { String day1 = ""; switch (convertNum) { case 1: day1 = "Sunday"; break; case 2: day1 = "Monday"; break; case 3: day1 = "Tuesday"; break; case 4: day1 = "Wednesday"; break; case 5: day1 = "Thursday"; break; case 6: day1 = "Friday"; break;
  • 16. case 7: day1 = "Saturday"; break; }// switch(convertNum) return day1; }//end of getDay //Where we convert the day of the week to a number public int getConvertDayToNum(String day) { int conversion = 0; switch (day) { case "Sunday": conversion = 1; break; case "Monday": conversion = 2; break; case "Tuesday": conversion = 3; break; case "Wednesday": conversion = 4; break; case "Thursday": conversion = 5; break; case "Friday": conversion = 6; break; case "Saturday": conversion = 7; break; }//end switch(day)
  • 17. return conversion; }//end of getNewDay public void calcPrevDay() //Made changes here to calculate the num from the day { int convertNum = getConvertDayToNum(day); String day1; int conversion; conversion = convertNum - 1; if (conversion == 0) { conversion = 7; } day1 = getConvertNumToDay(conversion); System.out.print(" The day before " + day + " is " + day1); }//end calcPrevDay() public void calcNextDay() { //Made changes here to calculate the num from the day int convertNum = getConvertDayToNum(day); String day1; int conversion; conversion = convertNum + 1; if (conversion == 8) { conversion = 1; } day1 = getConvertNumToDay(conversion); System.out.print(" The day after " + day + " is " + day1); }//end calcNextDay() public void calcNewDay(int addOnDays)
  • 18. { //Made changes here to calculate the num from the day int convertNum = getConvertDayToNum(day);; String newDay = ""; int totDaysNum = 0; totDaysNum = convertNum + addOnDays % 7; if (totDaysNum > 7) { totDaysNum = totDaysNum % 7; } newDay = getConvertNumToDay(totDaysNum); System.out.print("A " + newDay + " is " + addOnDays + " days past " + day + " "); }//end calcNewDay public void printDay() { System.out.print("The Current Day is " + day + " "); }//end printDay() }//end of class Day testDay2.java package array; import java.util.*; public class testDay2 { static Scanner console = new Scanner (System.in); public static void main (String[]args) { //declare local variable String day = ""; int convertNum = 0; int addOnDays = 0; Day2 day1 = new Day2(); Day2 day2 = new Day2("Tuesday");
  • 19. System.out.print("Output for day1: "); day1.printDay(); System.out.println(); System.out.print("Output for day2: "); convertNum = day2.getConvertDayToNum(day); day2.printDay(); System.out.print(" New output for day1: "); //Made changed here to convert from num to day since day to num is now invalid day1.setConvertNumToDay(6); day1.printDay(); System.out.print(" Output for day2: "); day = console.nextLine(); day2.calcNextDay(); day2.calcPrevDay(); System.out.print(" Enter the number of days from Friday: "); addOnDays = console.nextInt(); day1.calcNewDay(addOnDays); System.out.print(" Enter the number of days from Tuesday: "); addOnDays = console.nextInt(); day2.calcNewDay(addOnDays); }//end main }//end class testDay OUTPUT: Output for day1: The Current Day is Output for day2: The Current Day is Tuesday
  • 20. New output for day1: The Current Day is Friday Output for day2: Tuesday The day after Tuesday is Wednesday The day before Tuesday is Monday Enter the number of days from Friday: 3 A Monday is 3 days past Friday Enter the number of days from Tuesday: 12 A Sunday is 12 days past Tuesday