creating class and methods

creating class and methods

View Answers

December 22, 2009 at 12:16 PM

Hi Friend,

Try the following code:

import java.util.*;

public class Computer{
static String name;
static int size;
static int speed;
static String make;
static double price;
static int quantity;
public Computer(String n,int s,int sp,String m,double p,int q){
name=n;
size=s;
speed=sp;
make=m;
price=p;
quantity=q;
}
public String getName(){
return name;
}
public int getSize(){
return size;
}
public int getSpeed(){
return speed;
}
public String getMake(){
return make;
}
public double getPrice(){
return price;
}
public int getQuantity(){
return quantity;
}
public void valid(){
if((price==0)||(price<0)){
System.out.println("Please enter price");
System.exit(0);
}
else if((quantity==0)||(quantity<0)){
System.out.println("Please enter quantity ");
System.exit(0);
}
else{
displayData();
}
}
public void displayData(){
List<Computer> list = new ArrayList<Computer>();
list.add(new Computer(name,size,speed,make,price,quantity));
for (Computer s : list){
System.out.println("Computer Name: "+s.getName()+ "\nRAM Size :"+s.getSize()+"\nProcessor Make: "+s.getMake()+"\nProcessor Speed: "+s.getSpeed()+"\nQuantity: "+s.getQuantity()+"\nTotal Price of Computers: "+s.getPrice());
}
}
public static void main(String[]args){
System.out.println("Enter details");
Scanner input=new Scanner(System.in);
name=input.next();
size=input.nextInt();
speed=input.nextInt();
make=input.next();
price=input.nextDouble();
quantity=input.nextInt();
Computer c=new Computer(name,size,speed,make,price,quantity);
c.valid();
}
}

Thanks









Related Tutorials/Questions & Answers:
creating class and methods - Java Beginners
creating class and methods  Create a class Computer that stores... of the Computers. This class contains following methods, - Constructor method... ( ) method that creates array of 4 objects of Computer class and that takes input
java object class methods
java object class methods  What are the methods in Object class?  There are lots of methods in object class. the list of some methods are as- clone equals wait finalize getClass hashCode notify notifyAll
Advertisements
to create a java class and methods
to create a java class and methods  How to create a java class without using the library class to represent linked lists of integers and also provide it with methods that can be used to reverse a list & append two lists.Also
Creating methods in servlets - JSP-Servlet
Creating methods in servlets  I created servlet and jsp file.I Instantiated 3 objects and Defined 2 methods in my servlet, first method should write...; /** * * @author mihael */ public class mlNewServlet3 extends HttpServlet
what is class methods in objective c
what is class methods in objective c  What is class methods in objective c? Explain the class method of objective c with the help of an example
what is class methods in objective c
what is class methods in objective c  What is class methods in objective c? Explain the class method of objective c with the help of an example
PHP list class methods
Function get_class_methods gives the all methods names of the given class. It takes input as class name and returns the array of the methods name PHP LIst Class Methods Example <?php class myclass{ function aa
Class AsyncEvent important methods
In this section, you will learn about the important methods of AsyncEvent Class
write a program to demonstrate wrapper class and its methods......
write a program to demonstrate wrapper class and its methods......  write a program to demonstrate wrapper class and its methods
variables and methods declared in abstract class is abstract or not
variables and methods declared in abstract class is abstract or not  variables and methods declared in abstract class is abstract
Abstract class,Abstract methods and classes
or the base class. Subclasses are derived to implement the methods inherited from... Abstract methods and classes     ... is used with methods and classes.  Abstract MethodADS_TO_REPLACE_1
Creating Class in JRuby
Creating Class in JRuby    ... that how one can define a class in JRuby and can define methods in class as well as can call them. This example is a small program that defines a simple class "
using class and methods - Java Beginners
using class and methods  Sir,Plz help me to write this below pgm. here is the question: Assume a table for annual examination results for 10... the following code: import java.util.*; public class Student{ int rollNo
Object Class Methods in Java
We are going to discus about Object Class Methods in Java... or indirectly. There are many methods defined in java.lang.Object class...(), wait(), etc Java object class methods are:- finalize() clone() equals
Getting Methods Information of a class
Getting Methods Information of a class   ... to retrieve information of all methods of a class (that included in the program... of the getMethods() method in more detail. Create an object of class. Now retrieve all
Abstract class or methods example-1
;Animal class"); // this     // ...;form Animal class");   } } class ...;called form BuzzwordAnimal class");   }  
new operator for creating the object of inner class
new operator for creating the object of inner class  Hi , The folliowing is my code : class Outer { class Inner{ void show... "); } } } class Test { public statis void main(String args
Writing code with multiple Class Methods and String Arrays
Writing code with multiple Class Methods and String Arrays  So what I... input. It has to have multiple classes so I will have a main class, and a class... it to do but I'm getting stuck on what to put in the main class "music.java
Writing code with multiple Class Methods and String Arrays
Writing code with multiple Class Methods and String Arrays  So what I... input. It has to have multiple classes so I will have a main class, and a class.... That's all I need it to do but I'm getting stuck on what to put in the main class
Can a abstract class be defined without any abstract methods?
Can a abstract class be defined without any abstract methods?   hi, Can a abstract class be defined without any abstract methods? thanks
What's the proper way to test a class with private methods using JUnit?
What's the proper way to test a class with private methods using JUnit?  What's the proper way to test a class with private methods using JUnit
methods
methods  PrintStream class has two formatting methods,what
methods
methods  PrintStream class has two formatting methods,what
methods
methods  PrintStream class has two formatting methods,what
Can I have multiple main() methods in the same class?
Can I have multiple main() methods in the same class?  Hi, Can I have multiple main() methods in the same class?   Hi, No, We cann't use multiple main() method within the same class. If we do this then the program
Creating Volume Bar Using Gauge Class
Creating Volume Bar Using Gauge Class       This example is all about creating a volume bar using Gauge class. We are creating the object of Gauge class that contains
Java :Thread Methods
Java :Thread Methods This section explains methods of Thread class. Thread Methods : Thread class provides many method to handle different thread... the temporal executing thread object.ADS_TO_REPLACE_4 Some other methods
Call class methods
Call class methods       This Example shows you how to call class method in velocity... through method  init(). 2: Create object of VelocityContext Class. 3
can we use scanner class,class , object and methods to get output without using constructor ????
can we use scanner class,class , object and methods to get output without using constructor ????  im getting error here..i hav used scanner class for the input at the run time ..and created the object to call the methods...why
creating new file with File class - JSP-Interview Questions
creating new file with File class   I have a file uploading concept in my project. For that, while creating a new file is it necessary to give... of FileOutputStream class. If we give the path as from the file uploading location will it work
Abstract class or methods example-2
;defined in the non abstract class } abstract class ...;runLion1(); // defined in the non abstract class
Class, Object and Methods
Class, Object and Methods       Class : Whatever we can see in this world all the things are a object... methods. Constructor is the method which name is same to the class
Abstract class or methods example-3
abstract class BaseAbstractClass {   abstract void baseMethod1();    // Definition done in DerivedClass class   void 
Java - Class, Object and Methods in Java
Java - Class, Object and Methods in Java   ... will see how to use the class, object and it's methods. This program uses... of a class . An object is a software bundle of variables and related methods
how to use StringTokenizer to retrieve the class name, attributes name and methods name from the Java Source Code
one problem which is retrieve the Class Name, Attributes Name and Methods Names...how to use StringTokenizer to retrieve the class name, attributes name and methods name from the Java Source Code  hi, I have done a program whereby
Given a list of methods for a stateful or stateless session bean class, define which of the following operations can be performed from each of those methods: SessionContext interface metho
Given a list of methods for a stateful or stateless session bean class... of those methods: SessionContext interface methods, UserTransaction methods, Java Naming and Directory Interface API (JNDI API) access to java
Creating Bean Class In JSF using NetBeans
Creating Bean Class In JSF using NetBeans  ... illustrates how to create the Bean Class. This is also called the Backing Bean Class. This bean class is used to store the values of the form components
String toLowerCase and toUppercase methods example
() methods example:- String class provides methods toUpperCase() and toLowerCase... to  lowercase. We have used these two methods in this example. user can see how to use these methods. Example:-ADS_TO_REPLACE_1 <?xml
Class
techniques. A class defines the properties of  objects and methods used... classes. A class defines the properties of the objects and the methods used... together with its class. Now we will learn, how to define a class having methods
Java call method from another class
; methods of another class. For this we have created two java files... methods inside the class MainClass, an object of the CallingMethod class... to access the methods of the class CallingMethod. With just the object name along
Creating Menu Using Canvas Class
Creating Menu Using Canvas Class       This example shows how to create the menu and call the canvas class to show the toggle message. The Toggle message will appear when
Access Static Member Of The Class Through Object
that work without any object of the class. Static methods are limited to calling other static methods in the class and to using only static variables... Access Static Member Of The Class Through Object  
class
medals. In this class, you should also define constructors, and assessor, mutator methods. Task 2 MedalTally.java is a class to model a medal tally, containing... is a class to represent a country in medal tally. It has an attribute of country
Methods of HttpServlet
Methods of HttpServlet  What are methods of HttpServlet
Class
Class, Object and Methods     ... the class, object and it's methods. This program uses the several values... methods of the special class. In the above example the sq is the object of square
class
class  Is a class a subclass of itself
creating a own interceptor - Struts
of defining the interceptor methods in the bean class. For more information...creating a own interceptor   i want to create own interceptor how i will create and which class i want implement  Hi friend
Using Abstract Class
class. This class must be inherited. Unlike interface the abstract class may implement some of the methods defined in the class, but in this class at least one...Using Abstract Class      
wrapper class
wrapper class  list wrapper class methods
wrapper class
wrapper class  wrapper class methods

Ads