-1
import java.awt.*;
import java.sql.*;
import java.awt.event.*;
public class shares 
{
 public static void main(String[] arguments){
String P[]=new String[100];
String Co[]=new String[100];
String N[]=new String[100];
        String Cu[]=new String[100];
String PA[]=new String[100];
String CoA[]=new String[100];
        String NA[]=new String[100];
        String CuA[]=new String[100];
         int number_bef=0;
         int number_aft=0;
         int Hayleys_Amount =0;
         try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cc = DriverManager.getConnection("jdbc:odbc:me");
Statement ss = cc.createStatement();
        ResultSet rec_bef = ss.executeQuery("select * from List order by Purchased_Date" );
while (rec_bef.next()) {
         P[number_bef]=rec_bef.getString("Purchased_Date");
         Co[number_bef]=rec_bef.getString("Company");
         N[number_bef]=rec_bef.getString("Number_of_Shares");
         Cu[number_bef]=rec_bef.getString("Current_Price");
         number_bef++;
         }
    rec_bef.close();

         ss.executeUpdate("insert into List  (Purchased_Date, Company, Number_of_Shares, Current_Price) values ('Dec 28, 2003', 'Vanik(NV)', '1350','2')"); 

    ss.executeUpdate("delete * from List where Purchased_Date='Jan 21, 2000'");

    ResultSet rec_aft = ss.executeQuery("select * from List order by Purchased_Date" );
while (rec_aft.next()) {
         PA[number_aft]=rec_aft.getString("Purchased_Date");
         CoA[number_aft]=rec_aft.getString("Company");
         NA[number_aft]=rec_aft.getString("Number_of_Shares");
         CuA[number_aft]=rec_aft.getString("Current_Price");
         number_aft++; 
         }
    rec_aft.close();
    }catch  (Exception e) {
    System.out.println("ERROR: " + e.toString() +e.getMessage()); 
    }
    System.out.println("           ");
    System.out.println("Purchased_Date, Company, Number_of_Shares,Current_Price"); 

    for (int i=0; i<number_bef; i++){
    System.out.println(P[i] +  " , "+ Co[i] + " , " + N[i] + " , "+ Cu[i] +  "." );
    }
    System.out.println("           ");
    System.out.println("Inserted ('Dec 28, 2003', 'Vanik(NV)', '1350','2') and deleted Purchased_Date='Jan 21, 2000' ");               

    // PRINTING AFTER UPDATE         
    System.out.println("Purchased_Date, Company, Number_of_Shares, Current_Price");  

       for (int i=0; i<number_aft; i++){
       System.out.println(PA[i] +  " , " + CoA[i] + " , " + NA[i] + " , " + CuA[i] +  "." );
       String str="Hayleys";
       if (CoA[i].equals(str))
         Hayleys_Amount = Hayleys_Amount  + (Integer.parseInt(NA[i])*Integer.parseInt(CuA[i]));

     }
    System.out.println("The current value of the Hayleys shares  = "  +Hayleys_Amount);
}
}

This is my code and when I try to run the applet through command prompt it says that shares cannot be cast to java.applet.Applet . Please help me to figure out what is wrong with my code. Thanks in advance.

user81883
  • 123
  • 2
  • 8
  • Why have you imported `java.awt.` and `java.awt.event`? I don't see the relevant code for these. – Paul Samsotha Nov 14 '13 at 14:11
  • 1) Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. 2) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). .. – Andrew Thompson Nov 14 '13 at 16:51
  • .. 3) Why AWT rather than Swing? See this answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. If you need to support older AWT based APIs, see [Mixing Heavyweight and Lightweight Components](http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html). – Andrew Thompson Nov 14 '13 at 16:51

2 Answers2

1

Your class needs to extend the java.applet.Applet class. Simply add extends Applet to the class definition (and also, of course, import java.applet.Applet). For example:

public class shares extends Applet {
   ..
}

Not related to the problem you have, you should follow the Java code conventions about naming, e.g. your class name should start with a capital letter.

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
0

shares cannot be cast to java.applet.Applet because the shares class is not a subclass of Applet. To resolve this you should use:

public class shares extends Applet{
Sionnach733
  • 4,686
  • 4
  • 36
  • 51