• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

First exercise

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1st Exercise:
Creating an abstract Superclass and concrete subclass
1.Create the Superclass as follows :

package food;
public abstract class Fruit{/* any code here */}

--> So I did this :
I created folder food and its path is C:\Program Files\Java\food\Fruit.java

Inside food folder ,I saved Fruit.java.

I wrote this code inside Fruit.java:


package food;
public abstract class Fruit{

}



When I tried compiling it gave me "Access Denied" error. So,I changed Fruit file location.
The new location is C:\Projects\Fruit.java
When I compiled,this time there was no error.
Hence,it proved its Windows restriction that did not compile inside Java folder.

In the first exercise,there is second question :
2]Create the subclass in a separate file as follows:
import food.Fruit;
class Apple extends Fruit {/*any code here*/}
As its mention create separate file so I created another folder named "Market" in C drive and Stored Apple.java inside it.
C:\Market\Apple.java

Inside Apple.java I have written this below code:
import food.Fruit;
class Apple extends Fruit{

}

When I compiled ,it gave me error as Package food does not exists.

So ,I changed Apple.java location to C: drive.
C:\Apple.java

I again got same error as Package food does not exists.

Can somebody tell me how to to solve this first exercise ,also if you can guide me how to overcome this Windows 8.1 "access denied"

I am stuck at first exercise itself .Kindly help me out to resolve the issue.

 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Geni d'cruz wrote:Hence,it proved its Windows restriction that did not compile inside Java folder.



Actually you got the error because of the "Program Files" folder

When you compile a class, the compiler should be able to find the classes you refer to. The compiler looks for referred classes on the classpath. In your case due to the folder structure you are using, the compiler is not able to find the Fruit class. You can use this simple directory structure and it will work.

C:\Projects\Apple.java
C:\Projects\food\Fruit.java

Now compile these as follows:

C:\Projects\>javac food\Fruit.java
C:\Projects\>javac Apple.java

This should work. When you compile the Apple.java, the compiler looks in the current directory for the food package, it finds the food folder then it looks for Fruit.class inside the food folder which it will find. This is the easiest approach you can take. In your current folder structure you'll have to give the classpath yourself to the javac command which can get confusing in the beginning...
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can try with the folder structure you have like this:

C:\Projects\food\Fruit.java
C:\Market\Apple.java


To compile Fruit.java
C:\Projects\>javac food\Fruit.java
OR
C:\Projects\food>javac Fruit.java

To compile Apple.java
C:\Market\>javac -cp .;C:\Projects Apple.java

The -cp argument to javac command tells the compiler to look for classes in Projects folder. There the compiler finds the food.Fruits class...
 
Geni d'cruz
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This below lines did not work for me.
To compile Apple.java
C:\Market\>javac -cp .;C:\Projects Apple.java

The -cp argument to javac command tells the compiler to look for classes in Projects folder. There the compiler finds the food.Fruits class...
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Geni d'cruz wrote:This below lines did not work for me.


What's the error you are getting? What's the directory structure you created?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic