The Artima Developer Community
Sponsored Link

Java Answers Forum
2 really easy questions

11 replies on 1 page. Most recent reply: Apr 13, 2003 9:01 PM by Greg

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 11 replies on 1 page
Greg

Posts: 7
Nickname: nobel
Registered: Apr, 2003

2 really easy questions Posted: Apr 12, 2003 6:07 PM
Reply to this message Reply
Advertisement
I am very poor when it comes to programming, but trying really hard to pass my exam, I have two quick questions I hope someone can answer for me please....

Implement the method sum that gives summation of the passed array. ie; the array has five elements 1,2,3,4,5, then 15 should be returned.

public int sum (int [] array)
{




}
________________________________________________________

and I have been trying to practice some code but keep getting errors.....

public class DebugTest3
{
public static void main (String args[])
{
One oneVar= new One();
int answer = oneVar.getResult(10);
System.out.println("Answer=" +answer);

public abstract class One()
{
public abstract void displayResult(int result);
public int getResult(int num)
{
return 2 * num;
}}


Anyone's help is greatly appreciated.....
Nobel...


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: 2 really easy questions Posted: Apr 12, 2003 6:54 PM
Reply to this message Reply
public class Summer{
    public static void main(String[] args){
        int[] firstFiveNumbers = {1, 2, 3, 4, 5};
        Summer summer = new Summer();
        System.out.println("The sum is: " + summer.sum(firstFiveNumbers));
    }
    
    public int sum(int[] array){
        int n = 0;
        for (int i = 0; i < array.length; i++){
            n = n + array[i];
        }
        return n;
    }
}
 

Greg

Posts: 7
Nickname: nobel
Registered: Apr, 2003

Re: 2 really easy questions Posted: Apr 12, 2003 7:06 PM
Reply to this message Reply
Thank you very much, much apreciated, I don't want to seem pushy, but did my code for the 2nd question work????

Greg

Posts: 7
Nickname: nobel
Registered: Apr, 2003

Re: 2 really easy questions Posted: Apr 12, 2003 7:10 PM
Reply to this message Reply
Summer summer = new Summer();

I don't understand this part of the code, could you please elaborate on it for me... Thank you very much

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: 2 really easy questions Posted: Apr 12, 2003 7:32 PM
Reply to this message Reply
You have to create an instance of the class in the main method so you can refer to a non-static method.

Greg

Posts: 7
Nickname: nobel
Registered: Apr, 2003

Re: 2 really easy questions Posted: Apr 12, 2003 7:34 PM
Reply to this message Reply
okay. I see, thank you....

Any luck with the code regarding Debugtest3?

That would really help me out alot..

Thanks....

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: 2 really easy questions Posted: Apr 12, 2003 7:43 PM
Reply to this message Reply
Since the class One is abstract, you can not instantiate it.
But you can extend it and provide a non abstract or concrete implementation of the displayResult method.
public class DebugTest3 extends One{
    public static void main (String args[]){
        DebugTest3 oneVar= new DebugTest3();
        int answer = oneVar.getResult(10);
        oneVar.displayResult(answer);
    }
    
    public void displayResult(int result){
        System.out.println("Answer = " + String.valueOf(result));
    } 
    
}
 

Greg

Posts: 7
Nickname: nobel
Registered: Apr, 2003

Re: 2 really easy questions Posted: Apr 12, 2003 7:52 PM
Reply to this message Reply
you really saved me...thank you very much Charles.....

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: 2 really easy questions Posted: Apr 12, 2003 7:56 PM
Reply to this message Reply
That's it for this old man for tonight.
Let me hear from you if there is anything else.

Charles
charbell@bellsouth.net

I've got a slew of java code samples from previous questions answered at:
http://www.quantumhyperspace.com/SourceBank/index.jsp

Its got an email feauture so you can email yourself anything you want.

Greg

Posts: 7
Nickname: nobel
Registered: Apr, 2003

Re: 2 really easy questions Posted: Apr 12, 2003 8:07 PM
Reply to this message Reply
Sorry to bug you again, but I ran the code and got errors

C:\Java\DebugTest3.java:7: cannot resolve symbol
symbol : class one
location: class DebugTest3
public class DebugTest3 extends One{
^
C:\Java\DebugTest3.java:12: cannot resolve symbol
symbol : method getResult (int)
location: class DebugTest3
int answer = oneVar.getResult(10);
^
2 errors

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: 2 really easy questions Posted: Apr 13, 2003 1:17 PM
Reply to this message Reply
First compile the following:

public abstract class One {
    
    public abstract void displayResult(int result);
        
    public int getResult(int num){
        return 2 * num;
    }
}


Then, the other class which depends on the above.
Make sure they are in the same directory.


public class DebugTest3 extends One{
    public static void main (String args[]){
        DebugTest3 oneVar= new DebugTest3();
        int answer = oneVar.getResult(10);
        oneVar.displayResult(answer);
    }
    
    public void displayResult(int result){
        System.out.println("Answer = " + String.valueOf(result));
 
    }   
}

Greg

Posts: 7
Nickname: nobel
Registered: Apr, 2003

Re: 2 really easy questions Posted: Apr 13, 2003 9:01 PM
Reply to this message Reply
Thank you very much Charles!!!!

Flat View: This topic has 11 replies on 1 page
Topic: explorer dsn by java Previous Topic   Next Topic Topic: Java problems with my program

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use