Please place your code ”Homework3Problem2.java” and ‘given supporting queue and
ID: 3821195 • Letter: P
Question
Please place your code ”Homework3Problem2.java” and ‘given supporting queue and stack .java files as needed on blackboared by the date given.
1 Problem 1
Extend either the array-based implementation or the linked-list based implmentation of the queue to include a public String toString() method that creates a String such that each element is seperated by a comma. For example in a basic queue storing the integers 1, 2, 3, and 4. The toString() method would return the following String:
1, 2, 3, 4
2 Problem 2
Create a program that takes in the a year from the user and outputs all dates of the format MMDDYYYY such that it is a palindrome. For example for the year 2050 the date 05022050 would be given as it is the date that is a palindrome. If no date exists state so. Some examples:
Explanation / Answer
Problem 1:
/*
* Java Program to Implement Queue
*/
import java.util.*;
/* Class arrayQueue */
class arrayQueue
{
protected int Queue[] ;
protected int front, rear, size, len;
/* Constructor */
public arrayQueue(int n)
{
size = n;
len = 0;
Queue = new int[size];
front = -1;
rear = -1;
}
/* Function to check if queue is empty */
public boolean isEmpty()
{
return front == -1;
}
/* Function to check if queue is full */
public boolean isFull()
{
return front==0 && rear == size -1 ;
}
/* Function to get the size of the queue */
public int getSize()
{
return len ;
}
/* Function to check the front element of the queue */
public int peek()
{
if (isEmpty())
throw new NoSuchElementException("Underflow Exception");
return Queue[front];
}
/* Function to insert an element to the queue */
public void insert(int i)
{
if (rear == -1)
{
front = 0;
rear = 0;
Queue[rear] = i;
}
else if (rear + 1 >= size)
throw new IndexOutOfBoundsException("Overflow Exception");
else if ( rear + 1 < size)
Queue[++rear] = i;
len++ ;
}
/* Function to remove front element from the queue */
public int remove()
{
if (isEmpty())
throw new NoSuchElementException("Underflow Exception");
else
{
len-- ;
int ele = Queue[front];
if ( front == rear)
{
front = -1;
rear = -1;
}
else
front++;
return ele;
}
}
//override toString method for
/* Function to display the status of the queue */
@Override
public String toString() {
StringBuilder result = new StringBuilder();
for (int i = front; i <= rear; i++)
{
result.append(Queue[i]);
if(i+1<=rear)
result.append(",");
}
return result.toString();
}
}
/* Class QueueImplement */
public class QueueImplement
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Array Queue Test ");
System.out.println("Enter Size of Integer Queue ");
int n = scan.nextInt();
/* creating object of class arrayQueue */
arrayQueue q = new arrayQueue(n);
/* Perform Queue Operations */
char ch;
do{
System.out.println(" Queue Operations");
System.out.println("1. insert");
System.out.println("2. remove");
System.out.println("3. peek");
System.out.println("4. check empty");
System.out.println("5. check full");
System.out.println("6. size");
System.out.println("7. display");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to insert");
try
{
q.insert( scan.nextInt() );
}
catch(Exception e)
{
System.out.println("Error : " +e.getMessage());
}
break;
case 2 :
try
{
System.out.println("Removed Element = "+q.remove());
}
catch(Exception e)
{
System.out.println("Error : " +e.getMessage());
}
break;
case 3 :
try
{
System.out.println("Peek Element = "+q.peek());
}
catch(Exception e)
{
System.out.println("Error : "+e.getMessage());
}
break;
case 4 :
System.out.println("Empty status = "+q.isEmpty());
break;
case 5 :
System.out.println("Full status = "+q.isFull());
break;
case 6 :
System.out.println("Size = "+ q.getSize());
break;
case 7 :
System.out.println(q) ;
break;
default : System.out.println("Wrong Entry ");
break;
}
/* display Queue */
System.out.println(" Do you want to continue (Type y or n) ");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}
----------------------------------------------------------------------------------------------
Problem 2:
import java.util.*;
class Date
{
private String Y;
public Date(String year)
{
Y=year;
}
public void generateDates(String startDate, String endDate) {
Calendar calStart = new GregorianCalendar(Integer.parseInt(startDate.substring(4)), Integer.parseInt(startDate
.substring(2, 4)) - 1, Integer.parseInt(startDate.substring(0, 2)));
Calendar calEnd = new GregorianCalendar(Integer.parseInt(endDate.substring(4)), Integer.parseInt(endDate
.substring(2, 4)) - 1, Integer.parseInt(endDate.substring(0, 2)));
for (int i = 1; i <= 12; i++) {
String month = String.valueOf(i);
if (month.length() == 1) {
month = "0" + month;
}
for (int j = 1; j <= 31; j++) {
String day = String.valueOf(j);
if (day.length() == 1) {
day = "0" + day;
}
String monthDay = month + day;
String year = new StringBuilder(monthDay).reverse().toString();
Calendar currentDate = new GregorianCalendar(Integer.parseInt(year), Integer.parseInt(month) - 1, 1);
if (Integer.parseInt(day) > checkMaxDays(currentDate)) {
continue;
}
if (currentDate.getTimeInMillis() < calStart.getTimeInMillis()
|| currentDate.getTimeInMillis() > calEnd.getTimeInMillis()) {
continue;
}
if(year.equals(Y))
System.out.println(monthDay + year);
}
}
}
private static int checkMaxDays(Calendar currentDate) {
return currentDate.getActualMaximum(Calendar.DAY_OF_MONTH);
}
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter the year: ");
String y = scan.nextLine();
Date d=new Date(y);
String startDate = "0101"+y;
String endDate = "1231"+y;
d.generateDates(startDate, endDate);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.