Monday, June 16, 2008

4.Write the Java codes to repeatedly ask the user to enter a name for 10 times. Each time the user keys in a name- it will display “Hello” followed by the name.
The following is a sample of the output:

Enter name: John
Hello John
Enter name: Mary
Hello Mary
Enter name: Jo
Hello Jo
...

This is what I did:

import java.util.Scanner;
public class AskName {
public static void main(String [ ] args) {
String name;

Scanner sc = new Scanner (System.in);
System.out.println ("Enter your name: ");
name = sc.nextLine();

for (i = 1- i==10- i++)
{
name += i;
}
System.out.println ("Hello " + name");
}
}

Are my workings correct? If wrong please do correct me and explain..Thanks.

I d already know C (not very in depth- but basic things like strings/ class...)

and I was wondering if anyone knows a [GOOD] VIDEO LECTURE regarding to learning other programming languages such as
1. Java
2. Python
3. Ruby
4. others you thought would be interesting or helpful to learn?

Create a new Java class SumEvenNumbers in project Practical9. Copy the content of SumNumbers.java from the previous practical to SumEvenNumbers.java. Modify the program such that it will only sum all even integers from 2 to the input value- inclusive. Print an error message if the input value is less than 2.

The SumNumbers.java:

import java.util.Scanner;
public class SumNumbers {
public static void main(String [] args){
Scanner sc = new Scanner(System.in);
int sum = 0;
int i- num;

System.out.print("Enter a number: ");
num = sc.nextInt();
for (i=1; i<=6; i++)
{
sum = sum + i;
}
System.out.println("Sum of the output is " + sum);
}
}
This is what I did:

import java.util.Scanner;
public class SumEvenNumbers {
public static void main(String [] args){
Scanner sc = new Scanner(System.in);
int sum = 0;
int i- num;

System.out.print("Enter a number: ");
num = sc.nextInt();
for (i=2; i==num; i++)
{
sum = sum + i;
}
System.out.println("Sum of the output is " + sum);
}
}

But i have no idea on how to print an error message...Also- when I tried to run the program- it says the output is 0...when I key in 4 as the input...o.o?

Hi

I have a public class called Database and another public class called Customers.

I ve this line in Database :
private Customer customers[ ];

And further down I ve this:
customers = new Customer [2];
customers[0] = new Customer ("Dave Jones");
customers[1] = new Customer ("Victoria");

I ve this method in Database:
public String getCustomerDetails () {
return customers.getCustomerDetails();
}

There s a already a method in Customers called getCutomerDetails.

However- I m unable to compile the code. There seems to be something wrong with the line: return customers.getCustomerDetails();

Any idea why?

I need help!

Write a program to read in a number and sum up all the numbers from 1 to that number. For e.g- if the user key in 6- then the output is 21 (1+2+3+4+5+6). Implement the code according to the pseudocode below. Use SumNumbers as the class name and create in project Practical9.

Algorithm in pseudocode:
SET sum to 0
READ num
FOR i = 1 TO num
sum = sum + i
PRINT sum

This is what I did:

import java.util.Scanner;
public class SumNumbers {
public static void main(String [] args){
Scanner sc = new Scanner(System.in);
int sum = 0;
int i- num;

System.out.print("Enter a number: ");
num = sc.nextInt();
for (i=1; i<=6; i++);
{
sum = sum + i;
}
System.out.println("Sum of the output is " + sum);
}
}

But it doesn t work..please help..

The output is supposed to be 21- instead I got 7..

This is in java language..

hmm- it doesn t work..i still get 7 as the output..O.O...
I see I see...I got it! Thank you!! (Ohya- you do not have to include that semi-colon- otherwise the program output will not be 21- instead it would be 7.

Thanks!! ^^
Ohno- what the hell am I talking about...I meant- I shouldnt have included that semi-colon...-.-

No comments: