Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners creating class and methods
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


sagorika
creating class and methods
1 Answer(s)      4 years and 10 months ago
Posted in : Java Beginners



Advertisement
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
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
Advertisements
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
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
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
Class AsyncEvent important methods
In this section, you will learn about the important methods of AsyncEvent Class
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
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
Abstract class,Abstract methods and classes
to implement the methods inherited from the abstract class (base class... Abstract methods and classes     ... is used with methods and classes.  Abstract Method An abstract method one
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
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
Abstract class or methods example-1
;Animal class"); // this     // ...;form Animal class");   } } class ...;called form BuzzwordAnimal class");   }  
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
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... will create a new instance of Employee class . Now we can access it's methods
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...; Class[] parameterTypes =   methods[i].getParameterTypes
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
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
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
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
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
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
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
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
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      ... is a software bundle of variables and related methods of the special class...) something related to the class's methods. Constructor is the method which name
Java :Thread Methods
Java :Thread Methods This section explains methods of Thread class. Thread Methods : Thread class provides many method to handle different thread... and allow other threads the temporal executing thread object. Some other methods
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
 

 

 

DMCA.com