Create a class called Factor that has a public method called calculateFactor. The method has two two arguments of type integer and checks whether the smaller of the two integers is a factor of the larger of the two integers. The method will return a factor or a zero. Write a main method which repeatedly reads in a positive integer n (exiting only when n is negative), and displays out the largest proper divisor of n (that is, the largest factor of n that is less than n itself).

This is what i have so far, i am getting errors with the factor f=new factor() and defining n. Also in the classFactor i think im making a mistake even though its not giving me any errors, can u guys help me out.

import java.io.*;
import java.util.*;

class Factor{
public int calculateFactor(int n1, int n2){
public Factor newFactor()
int factor;
factor=n2%n1;
if(factor==0)
return factor;
else {
return 0;
}//end public
}//end class

public class lab7_1 {
public int main(String[]args){
System.out.print("input an integer number:");
Factor f= newFactor();
int n;
n=in.nextInt();
int i;
i=2;
while(i<=n)
calculateFactor(i,n);
System.out.print("largest divisor");
}

}

Post your code in code tags. When you click on the code tag icon, Change it from

to [CODE=Java]. Also, your code seems to have numerous errors, so I will get to that.

[CODE=Java]class Factor{
public int calculateFactor(int n1, int n2){
public Factor newFactor()
int factor;
factor=n2%n1;
if(factor==0)
return factor;
else {
return 0;
}//end public
}//end class

Your line public Factor newFactor() has numerous errors with it. First, an Object is declared like this (using String as the class in this example): String str = new String();. Your declaration doesn't have a semicolon at the end and does not have the correct syntax. Second, since you are in a method, using the word 'public' before you create the Object is not necessary and doesn't make sense. The Object can only be used inside of that method.

It should say: Factor f = new Factor();

I also noticed that you are not using the Factor Object you created, so what is the point of creating it?

im confused, are those my errors?

oh nevermind, i understand thats the way im supposed to post the program

Yes. An Object is created like this:

Classname variableName = new Classname();

In your case, that means

Factor whateverYouWantToCallItGoesHere = new Factor();

okay, but now im getting multiple errors

okay, but now im getting multiple errors

We can't see what you've done since the last post, so please post the updated code and an updated description of the problems.

okay

classFactor{
public int calculateFactor(int n1, int n2){
int factor;
if(factor==0)
	return 0;
else {
return factor;
}//end public
}//end class

public class lab7_1 {
public int main(String[]args){
System.out.print("input an integer number:");
Factor f= new Factor();
int n;
n=in.nextInt();
int i;
i=2;
while(i<=n)
calculateFactor(i,n);
System.out.print("largest divisor");
}

}

im getting errors with the calculate factor(i,n)
n=in.nextInt();
classFactor{
Factor f=new Factor();
Also i dont know how to accurately input the module to figure out if it is a divisor or not

classFactor{
public int calculateFactor(int n1, int n2){
int factor;
if(factor==0)
	return 0;
else {
return factor;
}//end public
}//end class

Line 1 - you need a space between class and Factor.
Lines 3 and 4 - factor isn't initialized and you are comparing it in an if statement. In C, you'd be allowed to do it (with potentially hazardous results), but I think Java gives you an error.

Change the Factor class to this:

class Factor{
public int calculateFactor(int n1, int n2){
int factor = 0;
if(factor==0)
	return 0;
else {
return factor;
}//end public
}//end class

and see if it at least compiles and runs.

As for the main function:

public int main(String[]args){
System.out.print("input an integer number:");
Factor f= new Factor();
int n;
n=in.nextInt();
int i;
i=2;
while(i<=n)
calculateFactor(i,n);
System.out.print("largest divisor");
}

Line 5 - I don't see a declaration for in , which I assume is a Scanner object, so you are going to run into trouble there.

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html

is there any way i can use a scanner in this program?

is there any way i can use a scanner in this program?

Sure, why not? The link I posted earlier has examples. Some of them are complex and don't apply to your program, but the very first one:

Scanner sc = new Scanner(System.in);
     int i = sc.nextInt();

does apply to you, and the example here may help too:
http://www.java2s.com/Tutorial/Java/0180__File/CreatingaScannerreadfromstandardinputScannerconinnewScannerSystemin.htm


Make sure to put this line at the top of your file so you can use Scanner:

import java.util.Scanner;

It's in the link above too. And here is another example that may be useful:

http://www.java-tips.org/java-se-tips/java.util/how-to-read-input-from-console.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.