6 Replies - 631 Views - Last Post: 15 January 2012 - 11:39 AM Rate Topic: -----

#1 iw2198  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-November 11

I have a question about applets

Posted 14 January 2012 - 06:37 PM

Hi,

I'm having a problem writing a very basic java applet. This is a homework question BUT I'm not just looking for the answer. This is for an O'Reilly SOT course and Im totally stuck. I had a friend of mine try to help. The simple code he was trying to show me would not work in the Code Runnrer 2 IDE I use on the O'Reilly site. He tried his code in the IDE he uses at his job and it was fine. Here is the question:

Create an applet that declares a float value. Set that float variable's value to 15 initially. Then perform mathematical calculations on the value using each of the following Java operators:
+
--
*=

Here is what I have tried:

 import java.applet.*;
import java.awt.*;
import java.lang.*;

public class MathApplet extends Applet {

    float a = 15;
    float x = 5;
    float y = 6;
    float z = 7;
    
    
    
    }
    
    public double int(integer x, y, z) {
    
        x = 5;
        y = 6;
        z = 7;
        System.out.println(a+x--*=y);
    }
} 


Here is the error code from the compiler:

MathApplet.java:16: class, interface, or enum expected
    public double
int(integer x, y, z) {
           ^
MathApplet.java:19: class,
interface, or enum expected
        y = 6;
       
^
MathApplet.java:20: class, interface, or enum expected
        z =
7;
        ^
MathApplet.java:21: class, interface, or enum
expected
        System.out.println(a+x--*=y);
       
^
MathApplet.java:22: class, interface, or enum expected
    }
   
^
5 errors


Everything I've looked up and read online makes little to no sense to me. I'm just looking for a little help. I really don't just want an answer. I just have no idea which way to go from here. An explanation on how to do a basic math problem in an applet and how to print the result would be great. I've tried reading the java tutorial on Oracle.com In the Numbers Classes doesn't make sense. Also looked up the java.lang package in the JavaTM 2 Platform, Standard Edition, v 1.3.1 API Specification page. Again makes no sense. All the math searches I have done on this site were also to complicated. Any help is much appreciated.

The code my friend tried which didnt cause errors in his compiler is here:

import java.applet.*;
import java.awt.*;
import java.lang.*;

public class MathApplet extends Applet {

    int a = 15;
    int b = 1;
    
    a = a + b;
    // a--;
    // a *= (float) 2; 
    System.out.println(a);
} 


And the error code my compiler gives for the code my friend wrote above:

MathApplet2.java:10: <identifier> expected
    a = a + b;
    
^
MathApplet2.java:13: <identifier> expected
   
System.out.println(a);
                      ^
MathApplet2.java:13:
<identifier> expected
    System.out.println(a);
              
         ^
3 errors
 


Thank you,
Al

Is This A Good Question/Topic? 0
  • +

Replies To: I have a question about applets

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: I have a question about applets

Posted 14 January 2012 - 06:56 PM

public double int(integer x, y, z) {

this method claims it will return a double value
it does not end by: return aDoubleValueOfSomeSort;

This is code
System.out.println(a);
code should be execute inside a method
it is in the middle of instace variables delcaration, not inside a method
Was This Post Helpful? 1
  • +
  • -

#3 iw2198  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-November 11

Re: I have a question about applets

Posted 14 January 2012 - 07:24 PM

View Postpbl, on 14 January 2012 - 06:56 PM, said:

public double int(integer x, y, z) {

this method claims it will return a double value
it does not end by: return aDoubleValueOfSomeSort;

This is code
System.out.println(a);
code should be execute inside a method
it is in the middle of instace variables delcaration, not inside a method


Thank you for your help. Please bear with me I'm a construction worker by trade not a programmer. I barely know what I wrote ( the code I mean ) So I basically called or created the Math Applet class. Then I think ( and guessing Im right in doing so) declared my variables. I assumed I had to call a method to do the actual math problem. I made it public and used int() because its a number problem. I used the "Double" (i dont know what its called when you put it between public and int() ) because my original error messages had double in them. So I added it in that method. I dont understand what it does Ok, maybe the hamster wheel is spinning, So your saying that i need to tell the method to return the answer to my math problem. then I would print the result of that return?

When you said

Quote

this method claims it will return a double value
do you mean literally. Like say a Double int ( 5+5) would return 20? ( I know thats not how to write it properly) =)

Thank you again,
Al
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: I have a question about applets

Posted 14 January 2012 - 10:50 PM

and the syntax is

public double methodName(int x, int y, int z) {

not

public double int(integer x, y, z) {

This post has been edited by pbl: 14 January 2012 - 10:51 PM

Was This Post Helpful? 1
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: I have a question about applets

Posted 15 January 2012 - 02:12 AM

Please don't take this as criticism. I'm trying to be helpful.

There are basic parts common to every Java program, class and method signatures, that you should know the names of and understand the purpose of. I know you weren't born knowing these things - none of us were - and learning Java is a collateral activity behind who knows how many others, but it's important to understand the basics and from that build a common language we can all use to talk about this cool thing.

I suggest you review Java basics in your textbook or here in the Oracle tutorials to understand better the basic parts of the obligatory "Hello World" Java application. There's also an equivalent "Hello World" Java applet tutorial to get you started with applets. The goal of your review is to build that foundation and common language while figuring out how to complete your Applet assignment.

I know it takes time and it's foreign. That's okay. We're patient and helpful here, but we also like to see new and old programmers get off on the right footing.

Quote

do you mean literally. Like say a Double int ( 5+5) would return 20?


Someday (soon, I hope), you'll think back to this question and chuckle. I'm going to leave it at that for now. Please come back when you're ready to continue the discussion.
Was This Post Helpful? 2
  • +
  • -

#6 iw2198  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 02-November 11

Re: I have a question about applets

Posted 15 January 2012 - 11:36 AM

View PostGregBrannon, on 15 January 2012 - 02:12 AM, said:

Please don't take this as criticism. I'm trying to be helpful.

I suggest you review Java basics in your textbook or here in the Oracle tutorials to understand better the basic parts of the obligatory "Hello World" Java application. There's also an equivalent "Hello World" Java applet tutorial to get you started with applets. The goal of your review is to build that foundation and common language while figuring out how to complete your Applet assignment.


I LOVE CRITICISM! pbl really helped get my gears turning again. Last night I was able to download my course in a .pdf which I transfered to my nook to reread. I will also check out your link above, thank you for that.

Quote

I know it takes time and it's foreign. That's okay. We're patient and helpful here, but we also like to see new and old programmers get off on the right footing.


I really appreciate the help. I wanted to ask this question about a month ago but I wanted to make sure I looked everywhere I knew to look. Plus I didn't want to get blasted for asking about an assignment. =) Thank you both for your kindness.

Quote

do you mean literally. Like say a Double int ( 5+5) would return 20?



Quote

Someday (soon, I hope), you'll think back to this question and chuckle. I'm going to leave it at that for now. Please come back when you're ready to continue the discussion.


I'll definitely be back after some much needed review. Thanks again. Btw, pbl my applet still doesn't work yet but I don't have any error codes! lol Thanks again for your help
Was This Post Helpful? 0
  • +
  • -

#7 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: I have a question about applets

Posted 15 January 2012 - 11:39 AM

Great attitude! Don't be gone long. Come back when you're ready to get that applet running, soon.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1