Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help with a Java Assignment
New Posts  All Forums:Forum Nav:

Help with a Java Assignment

post #1 of 17
Thread Starter 
I'm taking an online Java course, which those of us who have experience with online courses know, there is very little direction, so, I need help! Here's the assignment:
Quote:
Objective: To practise use of if-else, switch, while, break, continue statements and ?: operator.
Steps:
(i) Create a new folder called 'assign41' under C:\Java\Lesson4. Copy HelloWorld.java from Lesson 1 into it, and rename the file to "Weather.java". Replace the word "HelloWorld" with "Weather". Replace the System.out line with instructions to declare variables, and display output as described below:
(ii) Declare in 'int' type variable called 'i' and initialize it with a value of 1. This variable represents the year, starting at Year 1.
(iii) Declare a 'double' type variable called 'temp' initialized to 0.0.
(iv) Declare a 'final double' type constant called 'ANNUAL_INCREASE', and assign it a value of 1.0.
(v) Declare an 'double' type variable called 'uncertainty' initialized to 0.0.
(vi) Declare an 'String' type variable called 'result' initialized to " ".
(vii) Using a System.out.println line, display the header: " Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".
(viii) Using a while(true) statment, loop over Year i from 1 thro 5, EXCEPT i = 3. Use a break statement to exit the loop as appropriate. When i = 3, it should skip the calculations and go the next iteration, so use a continue statement for that.
(ix) In each iteration of the while loop, assign a value to 'uncertainty' using a switch statement and the following table:
Year (i) Uncertainty (Percent)
1 2.1
2 4.3
4 7.4
5 8.6
(x) Next, use the following formula to compute the temperature of each of the 12 months of years 1, 2, 4 and 5. Use a for loop to loop over the months in a given year.
Year i = 1:
Month j = 1, 2, ... 12
Note: (A) and (B) below, taken together will be called "Equation 1".
(A) Winter Months: j = 1, 2, 11 and 12: Temperature = j + 3
(B) Other Months: j = 3, 4, 5, .., 9, 10: Temperature = 5j + 7

Note: Use a logical expression to differentiate between Winter and Other Months, in conjunction with the ?: operator, all in one single statement to compute the temperature for a month j using Equation 1.

1st year, so ANNUAL_INCREASE doesn't apply, but uncertainty does. In the next line, increase Temperature by Uncertainty percent to display the final temperature, for each month.

Example: If Temperature calculated above is 45.0, and uncertainty is 2.1 percent, then final Temperature is 45.0 * (1.0 + 2.1/100) = 45.945. Only the integer part is to be displayed (decimal part truncated), so it should show 45 as the value in this case.
Years: i = 2, 4, 5
First calculate Temperature using Equation 1. Next, find the increases to apply to that value. Temperature increase per year: ANNUAL_INCREASE (degrees) - same increase applies to each month of a year. Use the Year i and ANNUAL_INCREASE in an expression to figure the exact increase that would apply for that year.

Uncertainty percent applies to the value obtained after the ANNUAL_INCREASE.

Example: If temperature for a month is 45.0 using Equation 1, and it is Year 4, so you are 3 years away from Year 1, which means 3 annual increases on top of each other. Each annual increase is 1.0 degree (= ANNUAL_INCREASE), so for 3 years beyond Year 1, you need 1.0 + 1.0 + 1.0 = 3.0 degrees from the base value (45.0), so temp for Year 4 would be 45.0 + 3.0 = 48.0.

Next, you apply the uncertainty percent on this modified value (48.0) as applicable for year 4 (7.4 percent). So the temperature for that month in Year 4 becomes: 48.0 X (1.0 + 7.4/100) = 51.552. Value displayed will be integer part only (decimal part truncated), so it should display 51 in this example.

Inside the 'for loop', concatenate temperature values for all 12 months with spaces in between, assign it to 'result' and display in one line along with the Year i. Repeat for years 1, 2, 4 and 5. Try to align the output with the Month headers by maniupulating the blank space in between.
(xi) A sample output session is shown below (your output needs to match with this):

C:\Java\Lesson4\assign41>javac Weather.java

C:\Java\Lesson4\assign41>java Weather
Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1 4 5 22 27 32 37 42 47 53 58 14 15
2 5 6 23 29 34 39 44 50 55 60 15 16
4 7 8 26 32 37 42 48 53 59 64 18 19
5 8 9 28 33 39 44 49 55 60 66 19 20
C:\Java\Lesson4\assign41>
(xii) Submit Weather.java.

So far, I have this :
Code:
class Weather
{
    public static void main(String[] args)
    {
        int i = 1;
                double temp = 0.0;
                final double ANNUAL_INCREASE = 1.0;
                double uncertainty = 0.0;
                String result = " ";
                while (i < 5)
                
                
                
                System.out.println(" Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
    }
}

I'm not really sure where to go with the "while" statement. Can I get an explanation as to how to apply this?
 
Work Lappy
(4 items)
 
 
CPUMotherboardGraphicsGraphics
Intel Core i7 960 P6T NVIDIA GeForce GTX 560 Ti NVIDIA GeForce GTX 560 Ti 
RAMRAMRAMHard Drive
G.Skill DDR3 G.Skill DDR3 G.Skill DDR3 OCZ Vertex 3 SSD 
Hard DriveOptical DriveCoolingOS
WD Backup Lite-On DVD Corsair H80 Windows 7 Pro 
KeyboardPowerCaseMouse
Logitech G15 v2 OCZ ModXStream Pro 700W Antec 300 Logitech g5 
Mouse PadAudio
Ratpad Razer Barracuda AC-1 
CPUMotherboardGraphicsRAM
Intel Core i5 0JGC48 Intel(R) HD Graphics Family Micron Technology  
  hide details  
Reply
 
Work Lappy
(4 items)
 
 
CPUMotherboardGraphicsGraphics
Intel Core i7 960 P6T NVIDIA GeForce GTX 560 Ti NVIDIA GeForce GTX 560 Ti 
RAMRAMRAMHard Drive
G.Skill DDR3 G.Skill DDR3 G.Skill DDR3 OCZ Vertex 3 SSD 
Hard DriveOptical DriveCoolingOS
WD Backup Lite-On DVD Corsair H80 Windows 7 Pro 
KeyboardPowerCaseMouse
Logitech G15 v2 OCZ ModXStream Pro 700W Antec 300 Logitech g5 
Mouse PadAudio
Ratpad Razer Barracuda AC-1 
CPUMotherboardGraphicsRAM
Intel Core i5 0JGC48 Intel(R) HD Graphics Family Micron Technology  
  hide details  
Reply
post #2 of 17
Thread Starter 
Yeah, I'm just making this progressively worse... redface.gif

Code:
class Weather
{
    public static void main(String[] args)
    {
        int i = 1;
                double temp = 0.0;
                final double ANNUAL_INCREASE = 1.0;
                double uncertainty = 0.0;
                String result = " ";
                while (i < 5)
                {
                if (i > 5) break
                if (==3) continue
                }
                
                
                
                System.out.println(" Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
    }
}
 
Work Lappy
(4 items)
 
 
CPUMotherboardGraphicsGraphics
Intel Core i7 960 P6T NVIDIA GeForce GTX 560 Ti NVIDIA GeForce GTX 560 Ti 
RAMRAMRAMHard Drive
G.Skill DDR3 G.Skill DDR3 G.Skill DDR3 OCZ Vertex 3 SSD 
Hard DriveOptical DriveCoolingOS
WD Backup Lite-On DVD Corsair H80 Windows 7 Pro 
KeyboardPowerCaseMouse
Logitech G15 v2 OCZ ModXStream Pro 700W Antec 300 Logitech g5 
Mouse PadAudio
Ratpad Razer Barracuda AC-1 
CPUMotherboardGraphicsRAM
Intel Core i5 0JGC48 Intel(R) HD Graphics Family Micron Technology  
  hide details  
Reply
 
Work Lappy
(4 items)
 
 
CPUMotherboardGraphicsGraphics
Intel Core i7 960 P6T NVIDIA GeForce GTX 560 Ti NVIDIA GeForce GTX 560 Ti 
RAMRAMRAMHard Drive
G.Skill DDR3 G.Skill DDR3 G.Skill DDR3 OCZ Vertex 3 SSD 
Hard DriveOptical DriveCoolingOS
WD Backup Lite-On DVD Corsair H80 Windows 7 Pro 
KeyboardPowerCaseMouse
Logitech G15 v2 OCZ ModXStream Pro 700W Antec 300 Logitech g5 
Mouse PadAudio
Ratpad Razer Barracuda AC-1 
CPUMotherboardGraphicsRAM
Intel Core i5 0JGC48 Intel(R) HD Graphics Family Micron Technology  
  hide details  
Reply
post #3 of 17
You need to increment i within your while loop.

If(==3) isnr comparing anything to 3.

Top of my head and thumb typed from phone
RAWR
(13 items)
 
Home Server
(11 items)
 
 
CPUMotherboardGraphicsRAM
i7-4790k msi z97 gaming 7 gtx 760 4gb 32GB GSkill  
Hard DriveHard DriveCoolingOS
Kingston HyperX 3K 120GB SSD Hitachi 2TB Cooler Master Hyper 212+ Windows 8.1 Pro 
MonitorKeyboardPowerCase
Vizio M492-b2 Logitech Wireless  Corsair HX620 NZXT Phantom White 
Mouse
Logitech Wireless 
  hide details  
Reply
RAWR
(13 items)
 
Home Server
(11 items)
 
 
CPUMotherboardGraphicsRAM
i7-4790k msi z97 gaming 7 gtx 760 4gb 32GB GSkill  
Hard DriveHard DriveCoolingOS
Kingston HyperX 3K 120GB SSD Hitachi 2TB Cooler Master Hyper 212+ Windows 8.1 Pro 
MonitorKeyboardPowerCase
Vizio M492-b2 Logitech Wireless  Corsair HX620 NZXT Phantom White 
Mouse
Logitech Wireless 
  hide details  
Reply
post #4 of 17
Thread Starter 
Ahh yeah, overlooked that... (i == 3) biggrin.gif
 
Work Lappy
(4 items)
 
 
CPUMotherboardGraphicsGraphics
Intel Core i7 960 P6T NVIDIA GeForce GTX 560 Ti NVIDIA GeForce GTX 560 Ti 
RAMRAMRAMHard Drive
G.Skill DDR3 G.Skill DDR3 G.Skill DDR3 OCZ Vertex 3 SSD 
Hard DriveOptical DriveCoolingOS
WD Backup Lite-On DVD Corsair H80 Windows 7 Pro 
KeyboardPowerCaseMouse
Logitech G15 v2 OCZ ModXStream Pro 700W Antec 300 Logitech g5 
Mouse PadAudio
Ratpad Razer Barracuda AC-1 
CPUMotherboardGraphicsRAM
Intel Core i5 0JGC48 Intel(R) HD Graphics Family Micron Technology  
  hide details  
Reply
 
Work Lappy
(4 items)
 
 
CPUMotherboardGraphicsGraphics
Intel Core i7 960 P6T NVIDIA GeForce GTX 560 Ti NVIDIA GeForce GTX 560 Ti 
RAMRAMRAMHard Drive
G.Skill DDR3 G.Skill DDR3 G.Skill DDR3 OCZ Vertex 3 SSD 
Hard DriveOptical DriveCoolingOS
WD Backup Lite-On DVD Corsair H80 Windows 7 Pro 
KeyboardPowerCaseMouse
Logitech G15 v2 OCZ ModXStream Pro 700W Antec 300 Logitech g5 
Mouse PadAudio
Ratpad Razer Barracuda AC-1 
CPUMotherboardGraphicsRAM
Intel Core i5 0JGC48 Intel(R) HD Graphics Family Micron Technology  
  hide details  
Reply
post #5 of 17
Code:
while (i < 5)
{
  if (i > 5) break
  if (i==3) continue
}

What's exactly the point of this code? Also you need to increase the i inside the loop, else it won't be able to exit

EDIT: Ok, read your assignment, still looks a weird way to code, by default when i gets to 5 it will quit by itself without needing the break.
Edited by Barbaroti - 2/20/12 at 6:01pm
First Build
(17 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k @4.5Ghz Gigabyte Z68X-UD7 B3 MSI 6950 G.Skill Sniper 2x4GB 
Hard DriveHard DriveHard DriveCooling
Crucial M4 64GB Samsung SpinPoint 250GB Hitachi 1TB Noctua NH-D14 
OSMonitorMonitorKeyboard
Windows Server 2012 Samsung S22B300 22' ViewSonic VA703B 17" CM Quickfire TK 
PowerCaseMouse
Corsair HX850 NZXT Phantom Mionix Naos 3200 
  hide details  
Reply
First Build
(17 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k @4.5Ghz Gigabyte Z68X-UD7 B3 MSI 6950 G.Skill Sniper 2x4GB 
Hard DriveHard DriveHard DriveCooling
Crucial M4 64GB Samsung SpinPoint 250GB Hitachi 1TB Noctua NH-D14 
OSMonitorMonitorKeyboard
Windows Server 2012 Samsung S22B300 22' ViewSonic VA703B 17" CM Quickfire TK 
PowerCaseMouse
Corsair HX850 NZXT Phantom Mionix Naos 3200 
  hide details  
Reply
post #6 of 17
Thread Starter 
I sometimes think the same....there are times when the assignments requirement is to do some extremely redundant things, even after we've learned a more efficient way to do it. It makes it really difficult.
 
Work Lappy
(4 items)
 
 
CPUMotherboardGraphicsGraphics
Intel Core i7 960 P6T NVIDIA GeForce GTX 560 Ti NVIDIA GeForce GTX 560 Ti 
RAMRAMRAMHard Drive
G.Skill DDR3 G.Skill DDR3 G.Skill DDR3 OCZ Vertex 3 SSD 
Hard DriveOptical DriveCoolingOS
WD Backup Lite-On DVD Corsair H80 Windows 7 Pro 
KeyboardPowerCaseMouse
Logitech G15 v2 OCZ ModXStream Pro 700W Antec 300 Logitech g5 
Mouse PadAudio
Ratpad Razer Barracuda AC-1 
CPUMotherboardGraphicsRAM
Intel Core i5 0JGC48 Intel(R) HD Graphics Family Micron Technology  
  hide details  
Reply
 
Work Lappy
(4 items)
 
 
CPUMotherboardGraphicsGraphics
Intel Core i7 960 P6T NVIDIA GeForce GTX 560 Ti NVIDIA GeForce GTX 560 Ti 
RAMRAMRAMHard Drive
G.Skill DDR3 G.Skill DDR3 G.Skill DDR3 OCZ Vertex 3 SSD 
Hard DriveOptical DriveCoolingOS
WD Backup Lite-On DVD Corsair H80 Windows 7 Pro 
KeyboardPowerCaseMouse
Logitech G15 v2 OCZ ModXStream Pro 700W Antec 300 Logitech g5 
Mouse PadAudio
Ratpad Razer Barracuda AC-1 
CPUMotherboardGraphicsRAM
Intel Core i5 0JGC48 Intel(R) HD Graphics Family Micron Technology  
  hide details  
Reply
post #7 of 17
BTW your while should be

while ( i <= 5 )

Since you need for it to enter on 5, which will be the 5th year. If you use <5, it will only include the numbers up to 4
First Build
(17 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k @4.5Ghz Gigabyte Z68X-UD7 B3 MSI 6950 G.Skill Sniper 2x4GB 
Hard DriveHard DriveHard DriveCooling
Crucial M4 64GB Samsung SpinPoint 250GB Hitachi 1TB Noctua NH-D14 
OSMonitorMonitorKeyboard
Windows Server 2012 Samsung S22B300 22' ViewSonic VA703B 17" CM Quickfire TK 
PowerCaseMouse
Corsair HX850 NZXT Phantom Mionix Naos 3200 
  hide details  
Reply
First Build
(17 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k @4.5Ghz Gigabyte Z68X-UD7 B3 MSI 6950 G.Skill Sniper 2x4GB 
Hard DriveHard DriveHard DriveCooling
Crucial M4 64GB Samsung SpinPoint 250GB Hitachi 1TB Noctua NH-D14 
OSMonitorMonitorKeyboard
Windows Server 2012 Samsung S22B300 22' ViewSonic VA703B 17" CM Quickfire TK 
PowerCaseMouse
Corsair HX850 NZXT Phantom Mionix Naos 3200 
  hide details  
Reply
post #8 of 17
Yeah, it doesn't help that the assignment is written almost incoherently.

Did you start on the "the stuff inside the while loop" yet?
RAWR
(13 items)
 
Home Server
(11 items)
 
 
CPUMotherboardGraphicsRAM
i7-4790k msi z97 gaming 7 gtx 760 4gb 32GB GSkill  
Hard DriveHard DriveCoolingOS
Kingston HyperX 3K 120GB SSD Hitachi 2TB Cooler Master Hyper 212+ Windows 8.1 Pro 
MonitorKeyboardPowerCase
Vizio M492-b2 Logitech Wireless  Corsair HX620 NZXT Phantom White 
Mouse
Logitech Wireless 
  hide details  
Reply
RAWR
(13 items)
 
Home Server
(11 items)
 
 
CPUMotherboardGraphicsRAM
i7-4790k msi z97 gaming 7 gtx 760 4gb 32GB GSkill  
Hard DriveHard DriveCoolingOS
Kingston HyperX 3K 120GB SSD Hitachi 2TB Cooler Master Hyper 212+ Windows 8.1 Pro 
MonitorKeyboardPowerCase
Vizio M492-b2 Logitech Wireless  Corsair HX620 NZXT Phantom White 
Mouse
Logitech Wireless 
  hide details  
Reply
post #9 of 17
Scratch that.

skkhai a few posts down has it.
Edited by Warfarin88 - 2/22/12 at 8:31am
post #10 of 17
I just read this assignment again and I want to backhand your teacher.
RAWR
(13 items)
 
Home Server
(11 items)
 
 
CPUMotherboardGraphicsRAM
i7-4790k msi z97 gaming 7 gtx 760 4gb 32GB GSkill  
Hard DriveHard DriveCoolingOS
Kingston HyperX 3K 120GB SSD Hitachi 2TB Cooler Master Hyper 212+ Windows 8.1 Pro 
MonitorKeyboardPowerCase
Vizio M492-b2 Logitech Wireless  Corsair HX620 NZXT Phantom White 
Mouse
Logitech Wireless 
  hide details  
Reply
RAWR
(13 items)
 
Home Server
(11 items)
 
 
CPUMotherboardGraphicsRAM
i7-4790k msi z97 gaming 7 gtx 760 4gb 32GB GSkill  
Hard DriveHard DriveCoolingOS
Kingston HyperX 3K 120GB SSD Hitachi 2TB Cooler Master Hyper 212+ Windows 8.1 Pro 
MonitorKeyboardPowerCase
Vizio M492-b2 Logitech Wireless  Corsair HX620 NZXT Phantom White 
Mouse
Logitech Wireless 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help with a Java Assignment