Java Method Overloading Ask For Two Names And Prints Three Different Greetings

Feb 24, 2014

The class Overloading below asks for two names and prints three different greetings. Your task is to write the class methods missing from the class declaration. Methods print the greetings as shown in the example print.

Hint:The names and parameter types of the needed methods can be checked from the main method because all methods are called there. This exercise also does not require you to copy the source code below to the return field.

The method declarations will suffice.

Example output
Type in the first name: John
Type in the second name: Doe

**********
Hi!
**********
Hi, John
**********
Hi, John and Doe
**********

import java.util.Scanner;
public class Overloading {
public static void main(String[] args) {
String firstName, secondName;

[Code] ....

View Replies


ADVERTISEMENT

Java Method Overloading

Feb 24, 2014

The class Overloading below asks for two names and prints three different greetings. Your task is to write the class methods missing from the class declaration. Methods print the greetings as shown in the example print. The names and parameter types of the needed methods can be checked from the main method because all methods are called there. This exercise also does not require you to copy the source code below to the return field. The method declarations will suffice.

Example output

Type in the first name: John

Type in the second name: Doe

Java Code:

import java.util.Scanner;
public class Overloading {
public static void main(String[] args) {
String firstName, secondName;
Scanner reader = new Scanner(System.in);

[code]....

View Replies View Related

Why Java Does Not Support Return Type In Method Overloading

Feb 7, 2015

why Java does not support return type in method overloading. I coded following and it compiles and runs without any errors.

class Ideone
{
public static void main (String[] args) {
sum(3,5);

[code]....

If Java did not support overloading based on return type, this program should not work, right?

View Replies View Related

Creates A Single Linked List That Stores Names And High Scores And Prints Them

Sep 29, 2014

a project I am working on. Its a program that creates a singly linked list that stores names and high scores and prints them. For some reason it is printing an entry extra times. Also my remove function is not working properly

GameEntry class:
package project;
public class GameEntry implements Comparable<GameEntry> {
private String name;
private int score;  
public GameEntry(String n, int s) {
name = n;

[Code]...

View Replies View Related

How To Return Array From A Method / Back Into Main Method That Prints Out Stuff

May 27, 2014

I'd like to know how to return a new array, I wrote in a method below the main method. I want to print the array but system.out.print doesn't work for arrays apparently. What structure i should use?

View Replies View Related

Why Operator Overloading Is Not Supported In Java

Oct 18, 2014

why operator overloading is not supported in Java.

View Replies View Related

Write A Method That Prints Characters

May 7, 2014

Write a method that prints characters using the following: public static void printChars(char ch1, char ch2, int numberPerLine). This method prints the characters between ch1 and ch2 with specific numbers per line. Characters are separated by exactly one space.Test your method with the following main method:

public static void main(String[] args)
{
printChars(‘A’,’z’,10);
printChars(‘0’,’9’,5);
}

View Replies View Related

Method Header For File Names?

Feb 27, 2015

I'm new to Methods and do not really know how to write a method header. What would an appropriate header look like for the following instance? getFilename: This method takes no parameters. It asks the user for a filename. If that file exists, it returns the filename. If it does not exist, it asks the user for another filename. It continues to ask for filenames until the user gives the name of a file that exists.

Also, how would I make a return tag for this and call it into the main method.

View Replies View Related

Method Header For File Names?

Feb 26, 2015

I'm new to Methods and do not really know how to write a method header. What would an appropriate header look like for the following instance? getFilename: This method takes no parameters. It asks the user for a filename. If that file exists, it returns the filename. If it does not exist, it asks the user for another filename. It continues to ask for filenames until the user gives the name of a file that exists.

Also, how would I make a return tag for this and call it into the main method.

View Replies View Related

Ambiguity When Implementing Multiple Interfaces With Same Method / Variable Names?

Oct 14, 2014

imagine you are implementing 2 interfaces having identical method signatures:

interface A {
void doStuff();
}
interface B {
void doStuff();

[Code] ....

How can I implement both methods?

Or another example with member variables:

interface A {
public static final int i = 3;
}
interface B {
public static final int i = 33;

[Code] ....

How can I go about making clear which 'i' is meant?

View Replies View Related

How To Write A Dot File That Prints Graph In Java

Dec 6, 2014

Let's say if I want to create a dot file that print this graph in Java, how do i go about it?

I'm starting with this:

PrintWriter writer = new PrintWriter("graph.dot");

View Replies View Related

Creating Method That Prints The Square Root Of A Number Up To A Certain Number?

Feb 27, 2015

I am trying to create a method that prints the square root of a number up to a certain number. It needs to take a single int parameter for example "n" , and then print all of the (positive) even perfect squares less than n, each on a separate line. I want the method to be called something like this:

public void Squares(int n) {
}

I need the output to look something like this:

Example: if n = 40, your code should print

4
16
36

So I have been working for a few hours now and am really stuck.

This is what I have so far:

int count = 0;
int n = 4;
int max = n;
while(count < max) {
System.out.println(n);
n = n * n;
count++;

View Replies View Related

Java Program That Prints Specific Numbers In Pattern

Dec 30, 2014

Write a java program that prints 0..........121..........23432..........3456543............456787654............56789098765 in this pattern?

My code:

public class Number {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int k=0;
for(int n=0; n<=10; n=n+2)

[code]....

View Replies View Related

Creating Java Program That Prints The Smallest Number

May 9, 2015

Here is my code :

import java.util.Scanner; 
public class smallestnumber
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in); 
int smallest =0;
int number;

[Code]...

here is the output of my code:

Enter the number
88
Enter the number
8
Enter the number
6
Enter the number
55

[Code]...

Why is it printing 0 instead of 1?

View Replies View Related

Java Program That Prints Out Taylor Series For Mathematical Constant E

May 10, 2015

I am trying to write a java program that prints out the number that is the mathematical constant e. As you input a number, the larger it gets , the closer it comes to 2.71828 . Here is my code:

//taylor series that prints out e^1=1+1/1!+1/2!+1/3!+.....
import java.util.Scanner;
public class taylor_1
{
public static void main(String args[]) {
 Scanner input=new Scanner(System.in);
int factorial =1;

[Code] .....

Here is the output of my code:

enter n
9
Taylor series is 9.0

View Replies View Related

How To Make A List Of Names With Java

Oct 16, 2014

One of my assignments was to make a program that would read a sequence of names and then list them all.Just to be clear, it would read them all first, and then it would list them all at the same time.

View Replies View Related

Format Of Java File Names

Mar 30, 2015

In Java® identifiers, you are allowed letters and numbers (also _ $£¢€ etc, but you should avoid them in normal identifiers). So you cannot have spaces. You cannot write public class Hello World because the javac tool will see World as a separate identifier and not understand what it means and will fail to compile the code. You must write public class HelloWorld instead. And because the class is labelled public you must call the source file Hello World. java. Since you can't have two classes with the same [fully‑qualified] name, you cannot write two public classes in the same source file.

“What about names of source files?” somebody will ask. Well, some file systems will permit spaces in file names; ext4 will and I suspect so will NTFS. Can you write file names with spaces in? You would have to have a different name of the class inside the file, because you can't have spaces, and you therefore cannot make the class public, but maybe you can write a package‑private class with a different name.

On ext4, you have to write out the name of the file and the shell will interpret the space as meaning there are two different file, so you have to escape the space.

campbell@campbellsComputer:~/java$ gedit My First Class.java
// My First Class.java
class Foo
{
public static void main(String... args)
{
System.out.println("Hello, World!");
}
}
campbell@campbellsComputer:~/java$ javac My First Class.java
campbell@campbellsComputer:~/java$ java Foo

View Replies View Related

Java Program That Keeps Track Of People Names

Jan 13, 2015

I need to make a program that keeps track of peoples names, allows you to add a note to each name and preferably would reorganize whatever you put in by date or by spelling. (I suppose the phonebook application in cell phones is a good match, a supermarket's list of foods and prices would work as well).

View Replies View Related

Write Java Program That Asks User For List Of Names?

May 21, 2015

I am trying to write a java program that asks the user for a list of names (one per line) until the user enters a blank line. At that point the program should print out the list of names entered, where each name is listed only once (i.e., uniquely) no matter how many times the user entered the name in the program. I am supposed to use an ArrayList for this problem.

My idea for a solution is to create an ArrayList<String>, read each name, i.e., line, entered and then to see if that name is already in the ArrayList. I created a for loop to check each element in the ArrayList but when I try to assign an element to a string variable I get the error "Type mismatch: cannot convert from Object to String". Not sure why that is happening because the ArrayList is defined as a String list.

Here is the code so far.

/*
* File: UniqueNames.java
*/
import acm.program.*;
import java.util.*;

[Code].....

View Replies View Related

What Is Overloading Constructors

Apr 6, 2014

Can you give me a simple description of what overloading a constructer is?

View Replies View Related

Overloading Variable Arguments

Dec 27, 2014

I am going through Thinking in Java, 4th Ed and I came across the section that talks about overloading variable arguments. I tried out a piece of code from the book. (Method and class names not exactly the same).

public class Varargs
{
public static void m1(Character... args)
{
System.out.println("Character");
 
[code]....

In the above code, the compiler throws an 'Ambiguous for the type varargs' error. The error goes away if the first method is changed to:

public static void m1(char c, Character... args)

why there is ambiguity in the first piece of code and not the second.

View Replies View Related

Overloading With Primitive Types?

Jan 22, 2015

Why the following is happening.

For the below code, when I execute it, it prints

Short method 10 //result 1
Sub class short method 10 //result 2

Which is as expected but if I comment out line 3, then it prints

Integer method 10 //result 3
Integer method 10 //result 4

I can understand result 3 is because of an upcast from short to int, since FunWithOverloading will not have a overloaded method with short now. However, what is happening with result 4? Shouldn't it call methodA of the subclass with the argument type short? If its because I have declared the reference variable, derived, of the type FunWithOverloading, then how come the first result correctly picks the overloaded method of the sub class?

class FunWithOverloading{
void methodA(int x){System.out.println("Integer method " + x);}
void methodA(short x){System.out.println("Short method " + x);} //line 3
} class OverloadedSubClass extends FunWithOverloading{
void methodA(short x){System.out.println("Sub class short method " + x);}

[Code] ....

View Replies View Related

Java Program That Specifies Three One-dimensional Arrays Names Price / Amount And Total

Dec 2, 2014

Recently I have missed a few weeks of my class due to family emergencies. Because of this I have missed very important lectures. My professor has assigned me the following assignment:

Prices Write a Java program that specifies three one-dimensional arrays names price, amount, and total. Each array should be capable of holding 10 elements. Using a loop, input values for the price and amount arrays. The entries in the total array should be the product of the corresponding values in the price and amount arrays. After all the data have been entered, display the following output:

Total Price Amount

==== ==== ======

Under each column heading display the appropriate value.

I know how to display everything using printf, how to create the actual arrays and define the array size, but I am confused on how and what loop to use and how to construct it. This is what I have managed to write up so far:

import java.util.Scanner; // Imports the Scanner class
import java.text.DecimalFormat; // Imports the DecimalFormat class

public class Prices
{
public static void main(String [] args)
{
double[] price = new double[10];
double[] amount = new double[10];
double[] total = new double[10];

// Create a new Scanner object
Scanner keyboard = new Scanner(System.in);
} //End of main method
} // End of public class

View Replies View Related

Bank Account With Methods / Inheritance And Overloading

Apr 23, 2014

What I have done wrong

public class BankAccount
{
String name;
int accountID;
double balance;
public void setAccount( String username, int ID, Boolean isJoint)

[Code] ....

View Replies View Related

Billing Class - Properly Overloading Operators

May 4, 2014

I was told to create a class named Billing that includes three overloaded computeBill methods for a photobook store.

The first method takes the price of the book ordered, adds 8% tax and returns the total due.
The second method takes the price of the book, and the quantity, adds tax and returns the total.
The final method takes the price, quantity and a coupon discount, adds tax and returns the total.

All of this I have managed fairly well, although I keep getting a strange error at the end of my program that the constructor is undefined. The problem bits of code(the one throwing the errors) are under the comment //Enter values into each of the methods

Code:

package org.CIS406.lab2;

public class Billing {
//Declarations
double bookPrice;
int quantityOrdered;
double couponValue;

[Code] ....

My first thought was to create a constructor for each of the methods that I am using...

View Replies View Related

Sorting ArrayList Of Type Int - Overloading Efficiency

Feb 11, 2014

I have a case in which I want to sort two types of ArrayLists (using quicksort) and the method originally coded only accepts a String ArrayList. The problem is that now I want to sort an ArrayList of type int but couldn't . . . so I decided to overload the method. Since it looks very ugly to copy and paste the same chunk of code only to change the method signature I wondered if there is a better way to make this method more dynamic and be able to take in different types of ArrayLists.

My code:

private ArrayList<String> sort(ArrayList<String> ar, int lo, int hi){
if (lo < hi){
int splitPoint = partition(ar, lo, hi);
sort(ar, lo, splitPoint);
sort(ar, splitPoint +1, hi);

[Code] .....

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved