-2

I've been working on a project recently and I have come to the end of my compilation, moving out of Eclipse and into cmd. I have a single .java names PA3 containing multiple classes.

My task is to compile the program with:

javac PA3.java

And to run the program with the command:

java PA3 test.dat

where test.dat is a placeholder (no need to worry about that)

When I compile, all the classes compile properly as shown: here

All I seem to run into is issues regarding

Error: Could not find or load main class PA3

Caused by: java.lang.ClassNotFoundException: PA3

And my code is below: (Mind you it is messy and a little over the place at the moment) '''

import linkedlist.MyPolygons;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


 public class PA3
{  
    //================================MAIN================================//
    public static void main(String[] args)
    {  
        MyPolygons.main(args);
    };
}

class MyPolygons{  
    //================================MAIN================================//
    public static void main(String[] args)
    {  
    MyPolygons poly = Import(new ExtendedFile("src\\linkedlist\\text.txt"));
    poly.display();
}    








//Represent a node of the doubly linked list  

class Node{  
    PlanarShape data;
    String type;  
    Node previous;  
    Node next;  

    public Node(PlanarShape data, String type) {  
        this.data = data;
        this.type = type;
    }  
}  

//Represent the head and tail of the doubly linked list  
Node head, tail = null;  

public void sort()
{
    Node current = head;
    boolean changed = true;
    while(changed)
    {
        changed = false;
        Node next;
        for(int i = 0; i < 50; i++)
        {
        current = head;
        while((next = current.next) != null) 
        {
            if(current.data.getArea(current.type) >  next.data.getArea(next.type))
            {
                //System.out.println("Current.data: " + current.data.getArea(current.type) + " Next.data: " + next.data.getArea(next.type));
                PlanarShape temp;
                temp = current.data;
                current.data = next.data;
                next.data = temp;
                
                String temps;
                temps = current.type;
                current.type = next.type;
                next.type = temps;
                current = next;
            }
            current = next;
        }
    
        }
    }
}

//addNode() will add a node to the list  
public void addNode(PlanarShape data, String type) {  
    //Create a new node  
    Node newNode = new Node(data, type);  

    //If list is empty  
    if(head == null) {  
        //Both head and tail will point to newNode  
        head = tail = newNode;  
        //head's previous will point to null  
        head.previous = null;  
        //tail's next will point to null, as it is the last node of the list  
        tail.next = null;  
    }  
    else {  
        //newNode will be added after tail such that tail's next will point to newNode  
        tail.next = newNode;  
        //newNode's previous will point to tail  
        newNode.previous = tail;  
        //newNode will become new tail  
        tail = newNode;  
        //As it is last node, tail's next will point to null  
        tail.next = null;  
    }  
}  
//display() will print out the nodes of the list  
public void display() 
{  
    
    //Node current will point to head  
    Node current = head;  
    if(head == null) 
    {  
        System.out.println("List is empty");  
        return;  
    }  
    System.out.println("Nodes of a Double linked list: ");
    System.out.println("Unsorted List: ");
    while(current != null) 
    {  
        System.out.print(current.type);
        System.out.print("[");
        //Prints each node by incrementing the pointer.  
        
        
        System.out.print(current.data); 
        if (current.type == "CIRC= ")
        {
            System.out.print(", ");
            System.out.print(getRad());
        }
        System.out.println(String.format("]: %.2f", current.data.getArea(current.type)));
        current = current.next;  
    } 
    sort();
    System.out.println("Sorted List: ");
    current = head;
    while(current != null) 
    {  
        System.out.print(current.type);
        System.out.print("[");
        //Prints each node by incrementing the pointer.  

        System.out.print(current.data);
        if (current.type == "CIRC= ")
        {
            System.out.print(", ");
            System.out.print(PlanarShape.returnRadius());
        }
        System.out.println(String.format("]: %.2f", current.data.getArea(current.type)));
        //System.out.println(current.data.originDistance());
        current = current.next;
    } 

}  

static float radius;
static float vertlength;
static PlanarShape PlanarShape = new PlanarShape();
public static MyPolygons Import(ExtendedFile f)
{  
    String[] lines = f.readLines();
    MyPolygons poly = new MyPolygons();
    for (int i = 0; i < lines.length; i++) 
    {
        PlanarShape PlanarShape = new PlanarShape();
        String[] array = lines[i].split(" ");
        String shapetype = array[0];
        switch(shapetype)
        {
        case "P":
            for(int k = 2; k < array.length-1; k++)
            {
                float x = Float.parseFloat(array[k]);
                k++;
                float y = Float.parseFloat(array[k]);
                PlanarShape.addVerticy(new Point(x, y));
                vertlength = ((array.length-2)/2);
            }
             
            //Add nodes to the list  
            poly.addNode(PlanarShape, "POLY= ");
            break;
            
        case "C":
            
            for(int k = 1; k < array.length-1; k++)
            {
                PlanarShape.addRadius(Float.parseFloat(array[array.length-1]));
                float x = Float.parseFloat(array[k]);
                k++;
                float y = Float.parseFloat(array[k]);
                PlanarShape.addVerticy(new Point(x, y));
            }
            poly.addNode(PlanarShape, "CIRC= ");
            break;
            
        case "S":
            for(int k = 1; k < array.length-1; k++)
            {
                float x = Float.parseFloat(array[k]);
                k++;
                float y = Float.parseFloat(array[k]);
                PlanarShape.addVerticy(new Point(x, y));
            }
             
            //Add nodes to the list  
            poly.addNode(PlanarShape, "SEMI= ");
            break;
        }

    }
    return poly;
}

public float getRad()
{
    
    return PlanarShape.returnRadius();
}

public float getLength()
{
    return vertlength;
}

} 


@SuppressWarnings("serial")
class ExtendedFile extends File 
{
    public ExtendedFile(String pathname) {
        super(pathname);
    }

public String[] readLines() 
{
    try 
    {
        FileReader fr = new FileReader(this);
        BufferedReader br = new BufferedReader(fr);
        Object[] buffer = br.lines().toArray();
        String[] lines = Arrays.copyOf(buffer, buffer.length, String[].class);
        fr.close(); br.close();
        return lines ;
    } 
    
    catch (IOException e) 
    {
        return new String[0];
    }
}
}

class Point
{
    float x;
    float y;

Point(float ex, float why)
{
    x = ex;
    y = why;
}

public String toString()
{
    return String.format("(%.2f , %.2f)", x, y);
}

public float getX()
{
    return x;
}
public float getY()
{
    return y;
}

public double dto()
{
    return Math.sqrt(Math.pow(x, 2)+Math.pow(y, 2));
}
}

class PlanarShape
{
List < Point > verticies =  new ArrayList < Point > ();
float radius;

public void addRadius(float rad)
{
    radius = rad;
}

public float returnRadius()
{
    
    return radius;
}

public void addVerticy(Point toAdd)
{
    verticies.add(toAdd);
}

public String toString()
{
    String buffer = "";
    for (int i = 0; i < verticies.size(); i++)
    {
        buffer += verticies.get(i).toString();
    }
    return buffer;
}

public Point[] getVerticies()
{
    Object[] buffer = verticies.toArray();
    return Arrays.copyOf(buffer, buffer.length, Point[].class);
}

public float polyarea;
public float circarea;
public float semiarea;

public float getArea(String type)
{
    float area = 0;
    
        switch(type)
        {
        case "POLY= ":
        {
            for(int n = 0; n < verticies.size(); n++) 
            {
                area += verticies.get(n).getY()*verticies.get(n == verticies.size()-1?0:n+1).getX() - verticies.get(n).getX()*verticies.get(n == verticies.size()-1?0:n+1).getY();
            }
            if(area < 0)
            {
                area = -area;
            }
            area /= 2;
            polyarea = area;
            break;
        }
        case "CIRC= ":
        {
            float pi = 3.14159265359F;

            float radius = returnRadius();
            
            area = pi * radius * radius;
            if(area < 0)
            {
                area = -area;
            }
            circarea = area;
            break;  
        }
        case "SEMI= ":
        {
            
            float x0 = verticies.get(0).getX();
            float y0 = verticies.get(0).getY();
            float x1 = verticies.get(1).getX();
            float y1 = verticies.get(1).getY();
            
            float y0y1;
            if ((y0 - y1) < 0)
                {
                    y0y1 = -(y0 - y1);
                }
            else
                {
                    y0y1 = (y0 - y1);
                }
            
            float x0x1;
            if ((x0 - x1) < 0)
                {
                    x0x1 = -(x0 - x1);
                }
            else
                {
                    x0x1 = (x0 - x1);
                }
            
            float x2 = x0 - y0y1;
            float x3 = x0 + y0y1;
            float y2 = y0 + x0x1;
            float y3 = y0 - x0x1;
            
            double diam = Math.sqrt((Math.pow((x2-x3), 2)) + ((Math.pow((y3 - y2), 2))));
            float radius2 = ((float)diam)/2;
            
            float pi = 3.14159265359F;

            area = (pi * radius2 * radius2)/2;
            if(area < 0)
                {
                    area = -area;
                }
            break;  
            
        }
        
    }
    
    return area;
}

public float returnArea(String type)
{
    switch(type)
    {
        case("POLY= "):
        {
            return polyarea;
        }
        case("CIRC= "):
        {
            return circarea;
        }
        case("SEMI= "):
        {
            return semiarea;
        }
    }
    return 0;
}
}

class SemiCircle
{
    List < Point > verticies =  new ArrayList < Point > ();

    public void addVerticy(Point toAdd)
{
    verticies.add(toAdd);
}

public String toString()
{
    String buffer = "";
    for (int i = 0; i < verticies.size(); i++)
    {
        buffer += verticies.get(i).toString();
    }
    return buffer;
}

public Point[] getVerticies()
{
    Object[] buffer = verticies.toArray();
    return Arrays.copyOf(buffer, buffer.length, Point[].class);
}

public float getArea()
{
    float area = 0;

    for(int n = 0; n < verticies.size(); n++) 
    {
        area += verticies.get(n).getY()*verticies.get(n == verticies.size()-1?0:n+1).getX() - verticies.get(n).getX()*verticies.get(n == verticies.size()-1?0:n+1).getY();
    }
    
    return area;
}

public double originDistance()
{
    double distance;
    distance = Math.sqrt((Math.pow((verticies.get(1).getX()),2))+(Math.pow((verticies.get(1).getY()),2)));
    System.out.println(distance);
    return distance;
}

}

class Polygon
{
List < Point > verticies =  new ArrayList < Point > ();

public void addVerticy(Point toAdd)
{
    verticies.add(toAdd);
}

public String toString()
{
    String buffer = "";
    for (int i = 0; i < verticies.size(); i++)
    {
        buffer += verticies.get(i).toString();
    }
    return buffer;
}

public Point[] getVerticies()
{
    Object[] buffer = verticies.toArray();
    return Arrays.copyOf(buffer, buffer.length, Point[].class);
}

public float getArea()
{
    float area = 0;

    for(int n = 0; n < verticies.size(); n++) 
    {
        area += verticies.get(n).getY()*verticies.get(n == verticies.size()-1?0:n+1).getX() - verticies.get(n).getX()*verticies.get(n == verticies.size()-1?0:n+1).getY();
    }
    if(area < 0)
    {
        area = -area;
    }
    area /= 2;
    return area;
}

public double originDistance()
{
    double distance = Math.sqrt(Math.pow((verticies.get(1).getX()), 2)+Math.pow((verticies.get(1).getY()), 2));
    System.out.println(distance);
    return distance;
}
}

class Circle
{

List < Point > verticies =  new ArrayList < Point > ();

public void addVerticy(Point toAdd)
{
    verticies.add(toAdd);
}

public String toString()
{
    String buffer = "";
    for (int i = 0; i < verticies.size(); i++)
    {
        buffer += verticies.get(i).toString();
    }
    return buffer;
}

public Point[] getVerticies()
{
    Object[] buffer = verticies.toArray();
    return Arrays.copyOf(buffer, buffer.length, Point[].class);
}

public float getArea()
{
    float pi = 3.14159265359F;
            
    MyPolygons polygon = new MyPolygons();
    float radius = polygon.getRad();
    float area = 0;
    
    area = pi * radius * radius;
    if(area < 0)
    {
        area = -area;
    }
        
    return area;
}

public double originDistance()
{
    double distance;
    distance = Math.sqrt((Math.pow((verticies.get(0).getX()),2))+(Math.pow((verticies.get(0).getY()),2)));
    System.out.println(distance);
    return distance;
}

}

Edit: Removing the 'linkedlist.package' and pulling the .java and .text files, they still retain the same error when compiling and running

Community
  • 1
  • 1

1 Answers1

1

If you want to run files that are in a package, make sure all your java files that are part of the package are in a folder named the package name (i.e. linkedlist).

Then, in the directory outside your package folder, do:

javac linkedlist/*.java
java linkedlist.PA3
Diana Shao
  • 61
  • 1
  • 8