• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

SOLVED Java NetBeans, :( I need help!

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Nkay

Registered
Joined
Feb 11, 2013
So, I have a couple of questions that I tried solving, and I wanted a heads up on how I should start, cause I have NO clue. Help me out?

Write a program that invites the user to input an IP number in the dotted decimal notation, and your program then computes and outputs the same IP number in the decimal notation on the screen. For example, if the user inputs the IP number 134.115.64.1, your program should output its decimal notation 2255699969. Your program should repeatedly ask the user to input the next IP number until the user wants to end. You may use the following formula to convert from the dotted decimal notation to decimal notation:
assume the IP number in the dotted decimal notation is a.b.c.d, where a, b, c, and d are decimal numbers between 0 and 255, the corresponding decimal number would be
(((a x 256) + b) x 256 + c) x 256 + d
Your program should check to make sure that the number the user typed in is a legitimate IP number. For instance, a number such as 136.301.44.240 is not a legitimate IP number because the second decimal, 301, is over 255. You should also design a sensible way for the user to signal the end of the
program (such as by typing the word “end”). Please note that there are no “negative” IP numbers. Therefore a number like -2299778952 is not an
accepted notation for any IP number. The program should be well-structured and should have a reasonable set of methods in addition to the
main method. It should use a good coding style, proper indentation, meaningful identifiers and appropriate comments throughout.


I can do this in one class or separate classes, doesn't matter.

I need a head start as soon as possible, have this due in less than 3 days!:bang head
 
I think you'll find once you get started this really isn't that bad. The way I'd recommend approaching it is first read up on using tokens with:

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/StringTokenizer.html

The real issue is dealing with the overflow. You can use type long or learn to use Java's BigInteger class

http://docs.oracle.com/javase/1.4.2/docs/api/java/math/BigInteger.html

oh and if you use the BigInteger class, to simplify things read up on bit shifting.

http://en.wikipedia.org/wiki/Bit_shifting

for dealing with the multiplication.
 
Though tokanizer is depreciated, so I'd just go with String.split() as that's the recommended way.

If you break the problem into pieces (usually the recommended thing to do), you'll notice you have a few things to figure out:

- How decimal notation of ip-addresses works (you already got a formula for that)
- What you need to check for when validating the IP address (you just really need 4 checks if you count ensuring they actually entered numbers a check)
- How to write a while loop that keeps querying the user for new input until they want to end

I'm not going to write it for you since it looks like an assignment, but if you run into a problem once you get started come back for help. You'll feel a lot better when you've solved it (mostly) by yourself, and it will also help you when you get difficult assignments. :)
 
import java.util.*;

public class IPAddress
{
public static String IP ;

public static void validateIPAddress()
{

System.out.println("Enter a valid IP address: xxxx.xxxx.xxxx.xxxx");
Scanner input = new Scanner(System.in);
IP = input.nextLine();


String[] tokens = IP.split("\\.");
if (tokens.length != 4)
{
System.out.println("Invalid IP Address length.");
}
for (String str : tokens)
{
int i = Integer.parseInt(str);
if ((i < 0) || (i > 255)) //checking negative and within range
{
System.out.println("Invalid IP Address range.");
}

}

System.out.println("Valid IP Address!");
System.out.println();
}


This is what I came up with so far, I know there is no main method yet, but this just splits the IP address.
I wanted to know, after I split the IP address into 4 octets, how to I save it into different strings? say, a,b,c and d
 
for (int x = 0; x < tokens.length; ++x)
{
System.out.println(x + tokens[x]);
}

GOT IT :D
Some how posting my questions makes me think harder :D
Thanks for the tips :)
It helps a lot :D:clap:
 
Yes. Question.
They told us to use this formula to get the subnet mask.
I'm not too sure what I need to do.
I've split the octects into a,b,c and d..
but how do I implement the formula?

(((a x 256) + b) x 256 + c) x 256 + d
 
Good work! Writing anything down tends to clarify it in your mind.

Is 0.0.0.0 a valid IP address? How about 255.255.255.255? I guess they are in theory, but probably pretty rare for a user to actually have one of them :)

You might also want to catch the possible NumberFormatException if one of the tokens can't be cast to an integer (one more check).

BTW, when pasting code to the forums you can wrap it in
Code:
 blocks to keep the intendation.
 
Yes. Question.
They told us to use this formula to get the subnet mask.
I'm not too sure what I need to do.
I've split the octects into a,b,c and d..
but how do I implement the formula?

(((a x 256) + b) x 256 + c) x 256 + d

a = tokens[0]
b = tokens[1]
...etc
 
Yeah, I got that. And I've done that part.
I wanted to know what exactly am I trying to get from this formula.
I didn't get the question :/

+

The answer would be an integer yes? (I'd say long, just to be safe)
 
That's actually a good question :)

A subnet mask looks like an IP address, but it shows the size of the network. If a machine needs to send a packet outside the network where it is, it will have to forward it to the gateway for further transport. If it needs to send it to a machine in the same subnet it can just communicate "directly" with that machine using mac-address rather then ip-address. Generally all broadcasts etc can also (when needed) be sent to all machines on the subnet.

That formula looks like it will output a long, which (if it works) would mean it would probably have to be converted into an ip-address, so the reverse of what you did while converting the ip to long.
 
*Edit: I'm deleting this because I was wrong and don't wish to cause confusion for people. I made a mistake on the format of my System.out.println() forgetting a set of parens that caused the final 1 to be concatenated to the String producing the erroneous results. A long works fine for this.
 
Last edited:
That's interesting, as you should be nowhere near the limits of Long. I'd have to check our actual classes, but we use Long for ip-addresses (bigint in database).
 
Thanks a lot guys, I have actually finished that question.
I'm doing this other question now, this is what it says.


"When all residents have been processed, the program is to print out the name of the city, the number of residents processed, the average annual income of residents and the average amount of tax due from all residents."

I can figure this question out.
I need help with "The number of residents processed."
I need to ask the user go enter a name for the resident and I need to print out the number of residents all together.
 
Design, write in Java, test and document a program to calculate the average tax that can be collected from residents of a city.
The following table is to be used to calculate the tax payable by each person on their annual income:
Tax Boundary Tax Due Excess %
$6,000 $0 15.0%
$37,000 $4,650 30.0%
$80,000 $17,550 37.0%
$180,000 $54,550 45.0%
As an example, assume a resident’s annual income is $30,000. His/her tax due based on annual income (using the above table) is
0 + ($30,000 - $6,000) * 15% = $3,600
If a resident’s annual income is $50,000, his/her tax due based on annual income is
4,650 + ($50,000 - $37000) * 30% = $8,550
Similarly, if a resident’s annual income is $90,000, his/her tax due based on annual income is
17,550 + ($90,000 - $80000) * 37% = $21,250
Your program should first ask the user to enter the four tax boundaries (an integer or a float each), the four tax dues (an integer or a float each), and the excess tax rates (for example 15.0% is entered as
0.15, a float or a double). You have the freedom to format the display on how you want to get the information from the user. It should then ask the user to enter the name of the city (a string). After the tax rate information and the city name have been entered, the annual income for each resident can be entered. For each resident, the program should ask the user to enter the name (a string) and his/her annual income. It should then calculate the tax due and display the name of the person, his/her annual income
and tax due (a float or a double). The program should loop around until the user does not want to process any more residents. Choose a sensible way for the user to indicate that there are no more residents to process. When all residents have been processed, the program is to print out the name of the city, the number of residents processed, the average annual income of residents and the average amount of tax due from all residents.
The program should be well-structured and should have a reasonable set of methods in addition to the main method. It should use a good coding style, proper indentation, meaningful identifiers and appropriate comments throughout. Your program should also include a method (eg, StudentInfo( )) to output your student details (name, student number, mode of enrolment, tutor name, tutorial attendance day and time) at the start of
program results. Devise suitable test data to test your program. The test data together with test results (sample program output) should be submitted as part of your external documentation.

That's the question, other thing I did not understand, do they want us to form our own look up table by asking the users to enter the numbers or do we somehow use the one they have already given us.
 
Other thing.
How do I call a method to main? (I know its the BASIC thing, but I'm confused)

A method with 3 parameters to the main.
 
From how I interpret the instructions it looks like the user is providing the tax boundaries and you aren't to hard code any of them. The big thing is checking the user input to make sure they don't give you any duplicate values for the boundaries and dues and you'll want to assume they can give you the values in any order which means you will need to sort everything when their done inputting them. You could sort them yourself or look at storing them in arrays and using something provided from Java's Arrays class to sort it for you.

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Arrays.html

Your last question I'm not sure I understand. You want to know how to call a method from another class that is in the class that contains your main method?

Code:
public class Someclass {

	public static void main(String[] args) {
		

	}
	public static void aMethod() {
		
	}

}

You would call it from another class with Someclass.aMethod().
 
Thanks a lot guys, I still need help with the "residents" part

"When all residents have been processed, the program is to print out the name of the city, the number of residents processed, the average annual income of residents and the average amount of tax due from all residents."

I can figure this question out.
I need help with "The number of residents processed."
I need to ask the user go enter a name for the resident and I need to print out the number of residents all together.
 
package tax;

import java.util.Scanner;

public class Tax
{
static String city;
static String name;
static float income;
static float taxDue, taxBoundary, excessTax;
static float taxBoundary1, taxBoundary2, taxBoundary3, taxBoundary4;
static float taxDue1, taxDue2, taxDue3, taxDue4;
static float excessTax1, excessTax2, excessTax3, excessTax4;
static float answer1, answer2, answer3, answer4;


private static void Student_Info()
{
System.out.println("Name:");
System.out.println("Student ID: ");
System.out.println("Enrollment Mode:");
System.out.println("Lecturer: ");
System.out.println("Tutorial:");
}

private static void Input()
{


Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter your first tax boundary: ");

taxBoundary1 = keyboard.nextFloat();
System.out.println("Please enter your second tax boundary: ");
taxBoundary2 = keyboard.nextFloat();
System.out.println("Please enter your third tax boundary: ");
taxBoundary3 = keyboard.nextFloat();
System.out.println("Please enter your fourth tax boundary: ");
taxBoundary4 = keyboard.nextFloat();
System.out.println("Please enter your first tax due: ");

taxDue1 = keyboard.nextFloat();
System.out.println("Please enter your second tax due: ");
taxDue2 = keyboard.nextFloat();
System.out.println("Please enter your third tax due: ");
taxDue3 = keyboard.nextFloat();
System.out.println("Please enter your fourth tax due: ");
taxDue4 = keyboard.nextFloat();

System.out.println("Please enter the first excess tax rate: (decimals)");
excessTax1 = keyboard.nextFloat();
System.out.println("Please enter the second excess tax rate: (decimals)");
excessTax2 = keyboard.nextFloat();
System.out.println("Please enter the third excess tax rate: (decimals)");
excessTax3 = keyboard.nextFloat();
System.out.println("Please enter the fourth excess tax rate: (decimals)");
excessTax4 = keyboard.nextFloat();

System.out.println("Please enter the name of your City: ");
city = keyboard.nextLine();
System.out.println("Please enter a resident's name: ");
name = keyboard.nextLine();
System.out.println("Please enter his/her annual income: ");
income = keyboard.nextFloat();

}


public static void Calculate1()
{

float difference = income - taxBoundary1;
float multiplication = difference*excessTax1;
answer1 = taxDue1 + multiplication;

System.out.println(answer1);
}


public static void Calculate2()
{

float difference = income - taxBoundary2;
float multiplication = difference*excessTax2;
answer2 = taxDue2 + multiplication;

System.out.println(answer2);
}


public static void Calculate3()
{

float difference = income - taxBoundary3;
float multiplication = difference*excessTax3;
answer3 = taxDue3 + multiplication;

System.out.println(answer3);
}

public static void Calculate4()
{

float difference = income - taxBoundary4;
float multiplication = difference*excessTax4;
answer4 = taxDue4 + multiplication;

System.out.println(answer4);
}



public static void main(String[] args)
{

Student_Info();
Input();


System.out.println("His/Her tax due based on annual income is: ");


if(income <= taxBoundary1 && income < 1)
{
Calculate1();
}
else if(income <= taxBoundary2 && income > taxBoundary1)
{
Calculate2();
}
else if(income <= taxBoundary3 && income > taxBoundary2)
{
Calculate3();

}
else
{
Calculate4();
}
}


}


This is what I've come up with so far.
I need to populate an array for the "Residents"
and also, here are some problems I'm facing:

1) I'm not asking the user to enter the name of a city, It isn't asking me, I don't know why. Please check on that.

2) The calculation seems to be wrong. For instance:
if the 4 tax boundaries are: 6000, 37000, 80000, 180000
the tax dues are: 0, 4650, 17550, 54550
and the excess tax is: 0.15, 0.30, 0.37, 0.45
respectively, the answer for Calculate1() should be 3600, I'm getting 2550. I am lost.
Help me out please :)
 
Oh. It works.
I guess I just need a LOT of time to figure code out :D

Thanks a lot guys <3
 
heyy NKAY.. im struck on the question that u discussed firstly in the thread,, the one with the IP address.. can u plzz help me out like post ur java code so i can get a little help?? i hav an assignment due this week so kindly help me out with this..
 
Back