473,383 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

How to add the two integers in this generic program.

Expand|Select|Wrap|Line Numbers
  1. /**
  2. * @(#)demoGen.java
  3. *
  4. * @version 1.00 2013/9/6
  5. */
  6.  
  7. public class demoGen {
  8.  
  9. public demoGen(T t1,T t2) {
  10. System.out.println(t1+t2);
  11. }
  12.  
  13. public static void main(String[] args) {
  14.  
  15. demoGen dem=new demoGen(10,15);
  16. demoGen demo=new demoGen("You are ","good");
  17.  
  18. }
  19. }
Aug 6 '13 #1
1 9076
chaarmann
785 Expert 512MB
Plus "+" is an overloaded operator. It concatenates strings, or adds numbers. The type of input is generic, so it is unclear how to compile it. Have you tried to compile your code? I bet it does not work. So you need to write one method for String and another for numbers. Using numbers, you can use "T extends Number" and instead of "t1 + t2", you can use "t1.doubleValue() + t2.doubleValue()".
If you want to have only a single method to handle all, you can define an interface with an "add(t1,t2)" method and implement it in a derived class (derived from String, Integer, Double etc.). Or an easier possibility, but not good code practice: instead of using generic types, define method arguments as "Object" and then add a line "if (t1 instanceof String && t2 instanceof String){result = (String)t1 + (String)t2" for each possible type. For example for adding integers: "if (t1 instanceof Integer && t2 instanceof Integer){result = (Integer)t1.intValue() + (Integer)t2.intValue()". (Maybe ".intValue()" is not needed with auto-in/outboxing, but I can't verify that at the moment)
Aug 7 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: radnus2004 | last post by:
main() { static int a = {1,2,3}; printf("&a-&a=%u\n",&a-&a); } If you compile the above program and run it prints 1. Why? I printed &a and &a separately and the difference
15
by: Brett Romero | last post by:
I have this class: public class MyClass<TValue> where TValue : IMyClass, new( ) {...} I need to create a class that will hold instances of the above class using List<>: public class...
5
by: hugo | last post by:
how to write a program that reads integers until end-of-file and then prints the maximum sum of consecutive values? example: if the input is 27 6 -50 21 -3 14 16 -8 42 33 -21 9 the output is ...
1
by: tamara omar | last post by:
i want u to check this main program if it is right? plz Enum error_code{overflow,under flow,success}; Const int maxstack=100; Template<class stacktype> Class stack{ Private: Stacktype entry ;...
4
by: Break2 | last post by:
I am trying to write a simple program which asks a user to enter 5 integers after which the average will be computed and written to the screen. That simple. However I want to do it according to a...
2
by: mika_ella258 | last post by:
the output should be like this Loop Program -------------------- enter 1st integer: enter 2nd integer: enter 3rd integer: enter 4th integer: enter 5th integer:
2
by: drea | last post by:
Array_A 9 2 0 56 3 70 18 21 22
1
by: yuva | last post by:
"Thank you for this amazing program. Within a weekend, my team increased with 50 new people joining. Every time I logged on there was a new affiliate. Absolutely amazing. " S. Horvath SOUTH AFRICA...
9
by: xiao | last post by:
It always dumped when I tried to run it... But it compiles OK. What I want to do is to do a test: Read information from a .dat file and then write it to another file. The original DAT file is...
0
by: ahren | last post by:
Why won't this actionscript program work? http://pastebin.com/xMmzfmmq http://pastebin.com/yMAKu5SH I think this code is from before i fixed some of it, but even when i fixed some, it would...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.