Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

HELP!!!...Calculations

Posted on 1998-07-27
5
Medium Priority
?
215 Views
Last Modified: 2010-05-18
HELP!!!
I have two write two very simple caculations (classes), but I am from the VB Area, so my Java is to say the least..the ****s. I am using JDK 1.1.6, in an MS-DOS environment.  I write my code in notepad then save it as a *.java file, then run javac filename.java
I know there is a better way, but for this purpose, I have to do it this way.

Anyway here are my two (simple) questions:

I need to write a little program that computes and prints the squares of the integers from 5 to 15.

Secondly I need write a second little program to calculate the sqaures and cubes of ten consecutive numbers.

Thanks in advance
0
Comment
Question by:UpAllNite
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 1228561
This looks like homework. We'd be happy to help, but it would
hardly be ethical, or helpful to do it for you. Please attempt a
solution, and ask us about specifics you are having trouble with.

0
 
LVL 1

Accepted Solution

by:
dryang earned 200 total points
ID: 1228562
ethical or not, UpAllNite has to learn some way or another. OK, this is the solution to your problems.

First Question.

//main class
public class calcs
{
      public static void main(String args[])
      {
            calcSquares cs = new calcSquares();
            cs.dotheCalc();
            }
      }
//
public class calcSquares
{
      int theNumber;      //declare a variable
      
      calcSquares() //no arg constructor
      {}
      
      public void dotheCalc()
      {
            System.out.println("Number\tThe Square");
            
            for(theNumber = 5;theNumber < 16;theNumber++)
            {
                  System.out.println(theNumber + "\t" + theNumber*theNumber);
                  }
            
      }      //method to do the calculation
            
      }
//

just run the JVM eg. java calcs and see the answer.

Question 2:
This is a more sophisticated version of Question 1 as it takes in the user input. The program will take in the String Array 'argv' and converts the Strings into an Integer Object. NO handling of primitive data until the calculation part. run it like this:

java SandQ 2 3 4 5 6 ....

it can handle more than 10 inputs.

//
public class SandQ
{
      public static void main(String argv[])
      {
            if(argv.length <= 0)
            {
                  System.out.println("SandQ <num1>...<numX>");
            }
            else
            {
                  System.out.println("Number\t\tSquare\t\tCube");
                  
                  for(int i=0;i<argv.length ;i++)
                  {
                        Integer intVal = new Integer(argv[i]);  //the input is String, so must convert
                        int theInt =intVal.intValue();
                        
                        System.out.println(theInt + "\t\t" + theInt*theInt + "\t\t" + theInt*theInt*theInt);
                        }
                  
                  }
                  
            }
      
}
//

Do check up your documentation regularly if you want to improve on your Java Programming. READ! READ! READ! Go to a library close to you and borrow some Java Programming Book. The ones by SUN Microsystems are the best.


0
 

Author Comment

by:UpAllNite
ID: 1228563
#1) Not of ethics, I could care less about Java at this point, I am a VB and SQL guy and have been for about 10 years.  

#2) I have contacts at SUN, which is only a couple miles away, and I have been personally been told too hold off on any major developments, because things are too murky, and there are "wars" going on, that most don't even know about.

#3)  All that said and done, answering a question, is hardly being unethical (homework or not), if I had a question like What is 2+2? ...and you told me 4, is that being unethical, I think not.

Anyway thanks for the answer.
0
 
LVL 1

Expert Comment

by:dryang
ID: 1228564
no problem man! am glad to help.   :)
0
 
LVL 16

Expert Comment

by:imladris
ID: 1228565
W.r.t. #3 from UpAllNite:

Answering a question in general, is not unethical (that's what we're here for).

Answering a specific question on homework is also fine (that's what a teacher or teaching assistant would do).

Answering the question: will you do this homework problem for me? With: yes here you go, most certainly is both unethical and unhelpful.

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question