Results 1 to 7 of 7
  1. #1
    pearllymary78 is offline Member
    Join Date
    Jun 2008
    Posts
    7
    Rep Power
    0

    Default How to create main class link to another two class?

    Hi, I wana learn how to create classes.

    Main Class name 'Sum_two_y':
    Get input x from user and send the input x to Class 1 and Class 2 to calculate and send back here to Sum up the two y value.

    Class 1 function as:
    y= x -2.5

    Class 2 function as:
    y= x/3.0

    Can write some codes to teach me?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    You'll learn better if you do it yourself and make all the mistakes we all have made in the learning process. Look at other code, read your text book and then type in some code and compile it.
    Come back here with your code and the errors you get and ask specific questions about them.

  3. #3
    pearllymary78 is offline Member
    Join Date
    Jun 2008
    Posts
    7
    Rep Power
    0

    Default

    Can you give me some samples or guides?

  4. #4
    pearllymary78 is offline Member
    Join Date
    Jun 2008
    Posts
    7
    Rep Power
    0

    Default

    I have make some search. Something like Inheritance?
    java.sun.com/docs/books/tutorial/java/concepts/inheritance.html
    I found a site and read, but where can I any tutorial examples ?
    My Java basic Java book is limited to creating single class.

  5. #5
    pearllymary78 is offline Member
    Join Date
    Jun 2008
    Posts
    7
    Rep Power
    0

    Default

    I found something...wait.
    website: javabeginner.com/java-class-inheritance.htm
    I will so something and come back to correct my mistake code.

  6. #6
    pearllymary78 is offline Member
    Join Date
    Jun 2008
    Posts
    7
    Rep Power
    0

    Default

    Java Code:
    class Vehicle {
    
    	// Instance fields
    	int noOfTyres; // no of tyres
    	private boolean accessories; // check if accessorees present or not
    	protected String brand; // Brand of the car
    	// Static fields
    	private static int counter; // No of Vehicle objects created
    	// Constructor
    	Vehicle() {
    		System.out.println("Constructor of the Super class called");
    		noOfTyres = 5;
    		accessories = true;
    		brand = "X";
    		counter++;
    	}
    	// Instance methods
    	public void switchOn() {
    		accessories = true;
    	}
    	public void switchOff() {
    		accessories = false;
    	}
    	public boolean isPresent() {
    		return accessories;
    	}
    	private void getBrand() {
    		System.out.println("Vehicle Brand: " + brand);
    	}
    	// Static methods
    	public static void getNoOfVehicles() {
    		System.out.println("Number of Vehicles: " + counter);
    	}
    }
    
    class Car extends Vehicle {
    
    	private int carNo = 10;
    	public void printCarInfo() {
    		System.out.println("Car number: " + carNo);
    		System.out.println("No of Tyres: " + noOfTyres); // Inherited.
    		//  System.out.println("accessories: "    + accessories); // Not Inherited.
    		System.out.println("accessories: " + isPresent()); // Inherited.
    		//        System.out.println("Brand: "     + getBrand());  // Not Inherited.
    		System.out.println("Brand: " + brand); // Inherited.
    		//  System.out.println("Counter: "    + counter);     // Not Inherited.
    		getNoOfVehicles(); // Inherited.
    	}
    }
    
    public class VehicleDetails { // (3)
    
    	public static void main(String[] args) {
    		new Car().printCarInfo();
    	}
    }
    Hi, can I know how many 'file' here in the code above?
    Can any1 teach me how to run and need to put the code in how many file?
    The output after run the program are:

    Constructor of the Super class called
    Car number: 10
    No of Tyres: 5
    accessories: true
    Brand: X
    Number of Vehicles: 1

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    You need one file for every public class .

    To run a program (it must have a public static main( method in it:
    Go to a commandline and enter:

    java <programname>

Similar Threads

  1. Replies: 2
    Last Post: 09-22-2011, 09:48 PM
  2. Could not find the main class, program will exit.
    By aryubi in forum New To Java
    Replies: 39
    Last Post: 02-19-2010, 10:02 AM
  3. JVM Could not find main class
    By banduskank in forum Advanced Java
    Replies: 2
    Last Post: 06-24-2008, 08:05 AM
  4. Calling a variable from main to another class
    By itsme in forum New To Java
    Replies: 1
    Last Post: 12-18-2007, 03:35 PM
  5. Main startup class
    By j0h@nb in forum New To Java
    Replies: 13
    Last Post: 11-30-2007, 05:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •