Hello I am new to Java and I have no idea where to start with this program. Can somebody please start me off or display how you would execute the code. Thank you. This is what I have so far, but it is just the basics:

/*
 * For each method, write the code clearly and concisely.
 * The array, dht, contains the highest temperature reached each day for days 0, 1,  ... 
 * dht.length = number of elements in array dht.
 */
public class Program1 {

	private static int[] dht;
	
	public static void main(String[] args) {

		initialize();
		System.out.println("highestTemp = " + highestTemp(dht));
		System.out.print("hottestDay = " + hottestDay(dht));

		// Add calls to remaining methods

	}
	
	public static void initialize() {
		dht = new int[7]; // dht.length = 7
		dht[0] = 41;
		dht[1] = 68;
		dht[2] = 77;
		dht[3] = 74;
		dht[4] = 74;
		dht[5] = 68;
		dht[6] = 88;
		// Extra challenge - do the work of the initiazlize all
		// in one Java statement.
	}
	
	/*
	 * Return the highest temperature reached during the recorded period.
	 * Don't print it out. Return it.  For dht above, return 88.
	 * You may assume for this method and all others that 
	 * temp.length >= 1
	 */
	public static int highestTemp( int[] temp ) {
// Fill in your code here
	}
	
	/*
	 * Return the index of hottest day. For dht above, return 6.
	 */
	public static int hottestDay( int[] temp ) {
// Fill in your code here
	}
	
	/*
	 * Count duplicate temperatures.
	 * For dht above, return 2. There is one duplicate each for 68 and 74.
	 */
	public static int countDuplicates( int[] temp) {
// Fill in your code here
	}
	
	/*
	 * Delete all duplicated temperatures. Compress the array to avoid
	 * gaps in the middle and pad out the array with the value -999 at the end.
	 * For dht above, change it to [41,68,77,74,88,-999,-999].
	 */
	public static void removeDuplicates( int[] temp ) {
// Fill in your code here	
	}
	
}

Recommended Answers

All 6 Replies

So you want someone to do your homework for you? If you had read the rules you would see that nobody will do your homework for you for less than $50

This is what I have so far:

/*
 * For each method, write the code clearly and concisely.
 * The array, dht, contains the highest temperature reached each day for days 0, 1,  ... 
 * dht.length = number of elements in array dht.
 */
public class Program1 
	{
	private static int[] dht;
	public static void main(String[] args) 
		{
        initialize();
		System.out.println("highestTemp = " + highestTemp(dht));
		System.out.print("hottestDay = " + hottestDay(dht));
		// Add calls to remaining methods
	}

	public static void initialize() 
	{
		dht = new int[7]; // dht.length = 7
		dht[0] = 41;
		dht[1] = 68;
		dht[2] = 77;
		dht[3] = 74;
		dht[4] = 74;
		dht[5] = 68;
		dht[6] = 88;
		// Extra challenge - do the work of the initiazlize all
		// in one Java statement.
	}

	/*
	 * Return the highest temperature reached during the recorded period. Don't
	 * print it out. Return it. For dht above, return 88. You may assume for
	 * this method and all others that temp.length >= 1
	 */
	public static int highestTemp(int [] temp) {
			//the highest temp' is the first element
			int highestTemp = dht[0];
			 
			//iterate from the second array (since we assume the first element is the highest)
			for (int i = 1; i < dht.length; i++) {
			   if (dht[i] > highestTemp) {
			         highestTemp = dht[i];
			   }
			}
			//return highest temp as int value
			return highestTemp;
			}

	/*
	 * Return the index of hottest day. For dht above, return 6.
	 */
	public static int hottestDay(int[] temp) 
	{
		// Fill in your code here
		int hottestDay = dht[0];

		for (int i = 1; i < dht.length; i++) 
		{
			if (dht[i] > hottestDay)
				hottestDay = dht[i];
		}
		return hottestDay;
	}

	/*
	 * Count duplicate temperatures. For dht above, return 2. There is one
	 * duplicate each for 68 and 74.
	 */
	public static void countDuplicates(int[] temp) 
	{
		// Fill in your code here
		int dht = temp[0];
		int count = 0;
		for (int i = 0; i < dht; i++)
			for (int x = 0; i < dht; i++) 
			{
				// if we've found a match, count it
				if (x == i)
					count++;
			}
	}

	/*
	 * Delete all duplicated temperatures. Compress the array to avoid gaps in
	 * the middle and pad out the array with the value -999 at the end. For dht
	 * above, change it to [41,68,77,74,88,-999,-999].
	 */
	public static void removeDuplicates(int temp) 
	{
		// Fill in your code here
		{
			int duplicates = 0;

			for (int i = 0; i < dht.length; i++) {
				for (int j = 0; j < dht.length; j++) {
					if (i == j)
						return;
				}
			}
		}
	}
}

Your hottestDay method does the same exact thing as highestTemp. Which variable represents the current day you are checking?

In checkDuplicates, your outer loop will only run once while the inner loop will run 48 times. The variable x will never change from 0. Use your previous for loops as a guide.

removeDuplicates will be easier once you get checkDuplicates working.

This is the code that I have to remove the duplicates, but I keep getting an error on the last line (temp.remove)

public static void removeDuplicates(int [] temp) 
	{
		// Fill in your code here
		for(int i = 0; i< temp.length-1; i++)
			if(temp[i] == temp[i+1])
				temp.remove[i];
}	
}

int arrays do not have a remove method. You may be thinking of the List class. You will have to implement that feature yourself. When you find a match, you will need another loop to shift the remaining elements one index forward. Also your matching logic is flawed. What if the duplicate temperatures are spaced apart?

This is the code that I have to remove the duplicates, but I keep getting an error on the last line (temp.remove)

public static void removeDuplicates(int [] temp) 
	{
		// Fill in your code here
		for(int i = 0; i< temp.length-1; i++)
			if(temp[i] == temp[i+1])
				temp.remove[i];
}	
}

Try this one. Its kind of messy, kinda sleepy already.. I used ArrayList to count the days since its the easiest way to actually output the index of an Element. I fixed your hottestDay() method becuase it should be returning the day of the week not the temperature. I used the index of the ArrayList as days of week + 1. I added 1 becuz you know.. you dont want to start at day 0. Anyways, my garbage work kinda organized the output for you. This should be really easy if you want to add more into it.
Good nyt everyone.

import java.util.*;


/*
 * For each method, write the code clearly and concisely.
 * The array, dht, contains the highest temperature reached each day for days 0, 1,  ...
 * dht.length = number of elements in array dht.
 */
public class Program1
	{
	private static int[] dht;
	public static void main(String[] args)
		{
        initialize();
        int count=0;
        //for Each Loop.
        for(int i: dht)
        System.out.println("day"+ ++count +" " +i+ "F");

		System.out.println("highestTemp = " + highestTemp(dht));
		System.out.println("hottestDay = Day " + hottestDay(dht));
		// Add calls to remaining methods
	}

	public static void initialize()
	{
		dht = new int[7]; // dht.length = 7
		for(int i=0; i < dht.length;i++)
		{
			dht[i]=(int) (Math.random() * 100);
		// Extra challenge - do the work of the initiazlize all
		// in one Java statement.
	    }
	}

	/*
	 * Return the highest temperature reached during the recorded period. Don't
	 * print it out. Return it. For dht above, return 88. You may assume for
	 * this method and all others that temp.length >= 1
	 */
	public static int highestTemp(int [] temp) {
			//the highest temp' is the first element
			int highestTemp = dht[0];

			//iterate from the second array (since we assume the first element is the highest)
			for (int i = 1; i < dht.length; i++) {
			   if (dht[i] > highestTemp) {
			         highestTemp = dht[i];
			   }
			}
			//return highest temp as int value
			return highestTemp;
			}

	/*
	 * Return the index of hottest day. For dht above, return 6.
	 */
	public static int hottestDay(int[] temp)
	{
		// Fill in your code here

		ArrayList<Integer> dayOfWeek= new ArrayList<Integer>();
		int hottestDay = dht[0];
        for(int s: dht)
         dayOfWeek.add(s);
		for (int i = 1; i < dht.length; i++)
		{
			if (dht[i] > hottestDay)

				hottestDay = dht[i];
        }
	     return dayOfWeek.indexOf(hottestDay)+1;

	}

	/*
	 * Count duplicate temperatures. For dht above, return 2. There is one
	 * duplicate each for 68 and 74.
	 */
	public static void countDuplicates(int[] temp)
	{
		// Fill in your code here
		int dht = temp[0];
		int count = 0;
		for (int i = 0; i < dht; i++)
			for (int x = 0; i < dht; i++)
			{
				// if we've found a match, count it
				if (x == i)
					count++;
			}
	}

	/*
	 * Delete all duplicated temperatures. Compress the array to avoid gaps in
	 * the middle and pad out the array with the value -999 at the end. For dht
	 * above, change it to [41,68,77,74,88,-999,-999].
	 */
	public static void removeDuplicates(int temp)
	{
		// Fill in your code here
		{
			int duplicates = 0;

			for (int i = 0; i < dht.length; i++) {
				for (int j = 0; j < dht.length; j++) {
					if (i == j)
						return;
				}
			}
		}
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.